本文介绍了Mongodb查询选择具有给定键的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我数据库中的记录是

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

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

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

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

我需要设计一个MongoDB查询来查找具有给定键的所有记录.

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

例如,如果我将ln作为参数传递给查询,它将shuold返回所有以ln为键的记录.结果将是

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"}

推荐答案

要查找文档中是否存在键/字段,请使用 $ exists 运算符.

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

通过MongoDB shell ...

Via the MongoDB shell ...

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

这篇关于Mongodb查询选择具有给定键的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 08:33