Umar Adil
Umar Adil

Reputation: 5277

How to search mongoId with array using php

My Mongo collection document is as:

Array
(
    [_id] => MongoId Object
        (
            [$id] => 52ddf885f786208bf58020df
        )
    [FirstName] => Aloma
    [State] => AR
    [Title] => AVP
    [Zip] => 71953
    [campaign_id] => Array
        (
            [0] => 52fba54fce798c441400002b
            [1] => 52fba687ce798c441400002c
        )

)

campaign_id is the array of mongoid's reference to other collection.

How can I get the documents with campaign_id = 52fba54fce798c441400002b Here we have to search mongoid 52fba54fce798c441400002b in array of campaign_id.

Upvotes: 0

Views: 71

Answers (1)

Nishu Tayal
Nishu Tayal

Reputation: 20840

You can use following code in mongoDB.

db.collection.find(campaign_id:[52fba54fce798c441400002b])

It will return the documents that contains [52fba54fce798c441400002b] as campaign_id value exactly.

For example :

{ "_id" : ObjectId("52fc4b93633d0c54e662fc75"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11 ] }
{ "_id" : ObjectId("52fc4b8d633d0c54e662fc74"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 12, 14 ] }
{ "_id" : ObjectId("52fc4b86633d0c54e662fc73"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11, 12, 14 ] }
{ "_id" : ObjectId("52fc4b7d633d0c54e662fc72"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11, 12 ] }

> db.scores.find({campaign_id:[11]})
{ "_id" : ObjectId("52fc4b93633d0c54e662fc75"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11 ] }

Same thing, you can implement in PHP using php mongo client.

I hope it solves your problem.

Upvotes: 1

Related Questions