Sunday, 15 March 2015

php - Mongo, match array of arrays -



php - Mongo, match array of arrays -

i have object has array of sub objects on it:

_id: "9", clients: [ { id: 677, enabled: true, updated: 0, created: 1352416600 }, { id: 668, enabled: true, updated: 0, created: 1352416600 } ], cloud: false, name: "love", }

the user makes request images client id 677, above object returned

the user makes request images client ids 677 , 668, image above returned

the user makes request images client ids 677, 668, 690, above image isn't returned

im using php , mongo db. mysql query used powerfulness used utilize count , sub query.

i have no thought start tackling in mongo.

any help appreciated.

to search documents within arrays, can utilize dot notation , "$and" operator.

syntax:

db.coll.find({"$and": [{"clients.id": <id1>}, {"clients.id": <id2>}, ... ]});

for samples:

1) user makes request images client id 677 (for 1 item, there no need of "$and", can utilize anyway):

db.coll.find({"clients.id": 677});

or

db.coll.find({"$and": [{"clients.id": 677}]});

2) user makes request images client ids 677 , 668:

db.coll.find({"$and": [{"clients.id": 677}, {"clients.id": 668}]});

3) user makes request images client ids 677, 668, 690:

db.coll.find({"$and": [{"clients.id": 677}, {"clients.id": 668}, {"clients.id": 690}]});

php mongodb

No comments:

Post a Comment