简体   繁体   English

使用 MongoDB 检查字段是否存在

[英]Check that Field Exists with MongoDB

So I'm attempting to find all records who have a field set and isn't null.所以我试图找到所有具有字段集但不是 null 的记录。

I try using $exists , however according to theMongoDB documentation, this query will return fields who equal null.我尝试使用$exists ,但是根据MongoDB 文档,此查询将返回等于 null 的字段。

$exists does match documents that contain the field that stores the null value. $exists确实匹配包含存储 null 值的字段的文档。

So I'm now assuming I'll have to do something like this:所以我现在假设我必须做这样的事情:

 db.collection.find({ "fieldToCheck": { $exists: true, $not: null } })

Whenever I try this however, I get the error [invalid use of $not] Anyone have an idea of how to query for this?然而,每当我尝试这个时,我都会收到错误[invalid use of $not]有人知道如何查询这个吗?

Use $ne (for "not equal")使用$ne (表示“不等于”)

 db.collection.find({ "fieldToCheck": { $ne: null } })

Suppose we have a collection like below:假设我们有一个如下的集合:

 { "_id":"1234" "open":"Yes" "things":{ "paper":1234 "bottle":"Available" "bottle_count":40 } }

We want to know if the bottle field is present or not?我们想知道瓶子字段是否存在?

Ans:答:

 db.products.find({"things.bottle":{"$exists":true}})

i find that this works for me我发现这对我有用

db.getCollection('collectionName').findOne({"fieldName": {$ne: null}})
db.<COLLECTION NAME>.find({ "<FIELD NAME>": { $exists: true, $ne: null } })

This comment is written in 2021 and applies for MongoDB 5.X and earlier versions.此注释写于2021 年,适用于 MongoDB 5.X 及更早版本。

If you value query performance never use $exists (or use it only when you have a sparse index over the field that is queried. the sparse index should match the criteria of the query, meaning, if searching for $exists:true , the sparse index should be over field:{$exist:true} , if you are querying where $exists:true the sparse index should be over field:{$exist:false}如果您重视查询性能,则永远不要使用 $exists (或仅在查询的字段上有稀疏索引时才使用它。稀疏索引应匹配查询的条件,这意味着,如果搜索$exists:true ,则稀疏索引应该在field:{$exist:true}上,如果你正在查询$exists:true稀疏索引应该在field:{$exist:false}

Instead use:而是使用:

 db.collection.find({ "fieldToCheck": { $ne: null } })

or或者

db.collection.find({ "fieldToCheck": { $eq: null } })

this will require that you include the fieldToCheck in every document of the collection, however - the performance will be vastly improved.这将要求您在集合的每个文档中包含 fieldToCheck,但是 - 性能将大大提高。

I Tried to convert it into boolean condition, where if document with table name already exist, then it will append in the same document, otherwise it will create one.我尝试将其转换为 boolean 条件,如果具有表名的文档已存在,则它将 append 在同一文档中,否则将创建一个。

table_name is the variable using which i am trying to find the document table_name 是我试图用来查找文档的变量

query = { table_name: {"$exists": "True"}} result = collection.find(query) flag = 0 for doc in result: collection.update_one({}, { "$push": { table_name: {'name':'hello'} } } ) flag = 1 if (flag == 0): collection.insert_one({ table_name: {'roll no': '20'}})

aggregate example聚合示例

https://mongoplayground.net/p/edbKil4Zvwc https://mongoplayground.net/p/edbKil4Zvwc

 db.collection.aggregate([ { "$match": { "finishedAt": { "$exists": true } } }, { "$unwind": "$tags" }, { "$match": { "$or": [ { "tags.name": "Singapore" }, { "tags.name": "ABC" } ] } }, { "$group": { "_id": null, "count": { "$sum": 1 } } } ])

You can use .Count() 您可以使用.Count()

count, err = collection.Find(bson.M{field: value}).Count()

but remember to set: 但请记住设置:

Session.SetSafe Session.SetSafe

db.SetSafe(&mgo.Safe{})

Otherwise it will return 0 on every call. 否则,它将在每次调用时返回0。

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

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