Search mongodb in PHP

Asked

Viewed 87 times

1

I am having trouble returning records through a date filter. I have the following JSON document:

{
        "_id": "111090485635468_1098582396886267",
        "1098582396886267_1098592810218559": {
            "fb_id": "1098582396886267_1098592810218559",
            "created_time": {
                "date": "2016-07-28 20:32:06.000000",
                "timezone_type": 1,
                "timezone": "+00:00"
            },
            "from": {
                "picture": {
                    "height": 261,
                    "is_silhouette": false,
                    "url": "https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-xaf1\/v\/t1.0-1\/p148x148\/13179438_970835299696887_7000909019670413123_n.jpg?oh=b4f9ecf8f00f8cb22a02874ec0bc7d96&oe=5815B673&__gda__=1479588815_6a2efe74184209e4b419783f02250c5d",
                    "width": 148
                },
                "name": "Jhonathan Vinicius",
                "id": "1020748971372186"
            },
            "message": "Uma string qualquer...",
            "comment_count": 7,
            "like_count": 11,
            "last_update": "29\/07\/2016 22:36:13"
        }

I need to filter all the records between dates, tried something like:

...
  $user = $c_users->find(   ['_id.fb_id.created_time.date' => [ '$gte'=> "2016-01-29 00:00", '$lt' => "2016-07-30 00:00" ] ]);  
....

And nothing comes back to me. I’m grateful to anyone who can collaborate!

  • If here [ '$gte' => ... the intention was to pass a value by the variable, change to [ '"{$gte}"' => ... . The same in $lt

  • Can you explain why this document structure? In my view 1098582396886267_1098592810218559 could be an array of objects... that id would be a post?

1 answer

1

Your reference to the field is wrong. Instead of _id.fb_id.created_time.date, use 1098582396886267_1098592810218559.created_time.date.

$user = $c_users
    ->find([
        '1098582396886267_1098592810218559.created_time.date' => [
            '$gte'=> '2016-01-29 00:00',
            '$lt' => '2016-07-30 00:00',
        ]
    ]);
  • I don’t know if it’s correct... _id is not an object

  • @gmsantos well noted, corrected the answer.

  • The problem is that this query will only work in this specific case. I see that the problem is lower, in the form that the object is modeled.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.