0

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.

2
  • documents ? what is your expected output ? Commented Feb 13, 2014 at 4:10
  • I need to get the count of documents in collection with campain_id array = new mongoid. Commented Feb 13, 2014 at 4:16

1 Answer 1

1

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.

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

Comments

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.