且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查MongoDB中数组的所有元素中是否存在字段,并返回包含该字段的文档

更新时间:2023-02-14 11:45:01

您可以使用$elemMatch查询运算符.没有直接查询运算符可以解决您的情况.

You can use $elemMatch query operator. There is no direct query operator to address your case.

db.collname.find( { "patient.drug":  { $not: { $elemMatch: { openfda: {$exists:false} } } } } )

"$elemMatch" + "$exists:false"

这部分包括patient.drug数组没有至少一个openfda嵌入式文档的所有文档.

This part includes all the documents where patient.drug array don't have at least one openfda embedded document.

$not

此部分将保留所有不在"$elemMatch" + "$exists:false"中的文档.

This part will keep the all the documents which are not in "$elemMatch" + "$exists:false".

所有具有所有drugs数组的文档都具有openfda嵌入式文档.

These are all the documents that has its all drugs array have openfda embedded doc.