简体   繁体   English

mongoDB:检查文档中是否存在嵌套对象

[英]mongoDB: check if existing nested object in document

I need a find query, which is searching for an existing attribute in an array.我需要一个查找查询,它正在搜索数组中的现有属性。

If this is the document...如果这是文件...

document文档

{
    id: '123',
    attr: 'anything',
    author: [
        { id: '1' },
        { id: '2' }
    ]
}

... I want to check if there is a document with an author id existing: ...我想检查是否存在具有作者 ID 的文档:

find query查找查询

var id = '123';
var uid = '2';

Collection.find({ _id: id, author: { id: uid } })

This should give me one result.这应该给我一个结果。

If uid = '3' there would be no result.如果uid = '3'将没有结果。

Use "dot notation" to denote embedded fields:使用“点符号”来表示嵌入的字段:

Collection.find({ "_id": id, "author.id": uid })

Otherwise you are asking for a document that has "exactly" and only a single object with the requested value.否则,您将要求一个“完全”且只有一个具有所请求值的对象的文档。 This just asks "does one possible value match".这只是询问“是否有一个可能的值匹配”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM