简体   繁体   English

如何检查字段是否存在,如果存在,在 mongodb $in 中获取其值?

[英]How to check if the field exists and if exists, get its value in mongodb $in?

I have the below query我有以下查询

db.getCollection('DOCUMENT1').find(                                     
{"FIELD1":           
    {                                                                   
            "$in":[
1234567,
7654321
            ]
    }
}       , "$and": [{
            "FIELD2" : { $exists : true }
    }] 
        ,{"FIELD1" : 1, "FIELD2" : 1} )

When I execute the above query, I get the below error:当我执行上述查询时,我收到以下错误:

Error: Line 9: Unexpected token:错误:第 9 行:意外的令牌:

I get only the documents that matches "Field1".我只得到与“Field1”匹配的文档。 Using $in, from the obtained document, I want to check if another field (FIELD2) exists and if exists, I want to display the value of that field.使用 $in,从获得的文档中,我想检查另一个字段 (FIELD2) 是否存在,如果存在,我想显示该字段的值。 How can I achieve that?我怎样才能做到这一点?

It should be something like this:它应该是这样的:

db.getCollection('YourCollectionName').find({$and :[{field1: {$in:[1234567, 7654321]}},{field2:{$exists: true}}]},
{field1:1, field2:1, _id:0})

Your query has syntax errors & also $and is an array that takes in multiple filters, which has to be given in place of filterQuery of .find() .您的查询有语法错误,而且$and是一个包含多个过滤器的数组,必须代替.find()的 filterQuery 给出。 Ex:- .find(filterQuery, projectQuery)例如:- .find(filterQuery, projectQuery)

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

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