billyJoe
billyJoe

Reputation: 2064

Get spécific fields with Mongodb

is there a way to get specific fields function of given parameters ? The idea is to return fields contained into POST variable.

I know i've to use projection, but there is no $in operator in projection.

i'd like something like :

db.getCollection("130637B1").find({$and:[{'node_id':1}, {'ctype': 'cpu'}]}, {'components.sensors.name': {$in: {'components.sensors.name':[request.POST]}}})

Any idea ?

Upvotes: 0

Views: 117

Answers (1)

Derick
Derick

Reputation: 36764

There is no $in operator for projections, but that doesn't mean you can't do this. The following query works just fine for example:

> db.so.drop();
true
> db.so.insert( { node_id: 42, ctype: 'cpu', components: { sensors: { name: 'foo', 'id' : '42bar' }, temp: 78.5 } } );
> db.so.find( {}, { ctype: 1, 'components.sensors.name' : 1 } );
{ "_id" : ObjectId("51dead724efd2a480aa6e6b1"), "ctype" : "cpu", "components" : { "sensors" : { "name" : "foo" } } }

I'm not exactly sure what your request.POST contains, so I can't give a more tailored answer.

Upvotes: 1

Related Questions