Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

Mongodb Query To select records having a given key

The records in my database are

{"_id":"1","fn":"sagar","ln":"Varpe"}

{"_id":"1","fn":"sag","score":"10"}

{"_id":"1","ln":"ln1","score":"10"}

{"_id":"1","ln":"ln2"} 

I need to design a MongoDB query to find all records that have a given key.

For example, if I pass ln as a parameter to the query it shuold return all records in which lnis a key. The results would be

{"_id":"1","fn":"sagar","ln":"Varpe"}

{"_id":"1","ln":"ln1","score":"10"}

{"_id":"1","ln":"ln2"} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To find if a key/field exists in your document use the $exists operator.

Via the MongoDB shell ...

db.things.find( { ln : { $exists : true } } );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...