0

I have created query in mongoDB. In MongoChef this query produces more than 10 thousand records in less than 2 seconds. Now I want to execute this query in PHP. So i don't know how to write query in php as I read various documents on internet but confused how to implement it.

 db.PMS.aggregate(
 [
    {$project:
       {EventTS:1,MainsPower:1,PanelID:1}
    },
    {$unwind: 
        {path:"$MainsPower",includeArrayIndex:"arrayIndex",preserveNullAndEmptyArrays:true}
    },
    { $match: { "MainsPower":{$ne:null}}},
    { $match: { "EventTS":{$gt:new Date("2016-01-01")}}},
    {$project:
        {MainsPower:1,
          PanelID:1,
          timestamp:{"$add":
                [{'$subtract' : ["$EventTS",new Date("1970-01-01")]},
                {"$multiply":[60000,"$arrayIndex"]}
                ]}
          }
    }
    ]
); 
2
  • Which driver are you using? Did you read this? php.net/manual/en/book.mongo.php Commented Jun 18, 2016 at 10:40
  • @m02ph3u5 I am using mongodb version : 1.1.7 Commented Jun 18, 2016 at 10:46

2 Answers 2

1

You can use some resources available on the php official documentation. A mapping of sql queries in php to mongoDB queries in php can be found here. Also I have a demo login and registration script at my github. You can view those in this repo.

Sign up to request clarification or add additional context in comments.

Comments

0

If you use MongoDB PHP Library you should be able to do something similar to this:

$mongo = new MongoClient();

$database = $mongo->examples;
$collection = $database->PMS;

$pipeline = [
    [
        '$project' => [
            'EventTS' => 1,
            'MainsPower'   => 1,
            'PanelID'   => 1,
        ]
    ],
    [
        '$unwind' => [
            'path' => '$MainsPower',
            'includeArrayIndex' => 'arrayIndex',
            'preserveNullAndEmptyArrays' => true

        ]
    ],
    ...
];

$cursor = $collection->aggregate($pipeline);

1 Comment

In $match i have to pass dynamic date so how to pass dynamic date variable in pipeline

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.