当前位置:网站首页>Query for Boolean field as "not true" (e.g. either false or non-existent)

Query for Boolean field as "not true" (e.g. either false or non-existent)

2022-07-05 00:49:00 Superior virtue and weak water

problem :

I'm sure I'm missing something very basic in MongoDB queries, can't seem to get this simple condition. I'm sure I'm MongoDB Some very basic things are missing from the query , It seems impossible to obtain such a simple condition .

Consider this collection Consider this set

> db.tests.find(){ "_id" : ObjectId("..."), "name" : "Test1" , "deleted" : true}{ "_id" : ObjectId("..."), "name" : "Test2" , "deleted" : false}{ "_id" : ObjectId("..."), "name" : "Test3" }

I would simply like to query all the items that are "not deleted" I just want to check all “ Not delete ” Project

I know how to find the item that has a "deleted" flag set to true: I know how to find “ deleted ” Flag set to true Project :

> db.tests.find({deleted:true}){ "_id" : ObjectId("..."), "name" : "Test1" , "deleted" : true}

But how do I find all items that are NOT "deleted" (eg negate the above query, or in other words, any items that either doesn't have a "deleted" field, or have it with value false But how did I find out , Are all the projects "deleted" ( For example, deny the above query , Or to put it another way , Without any items "deleted" field , Or value has it false

What I tried by guessing (please don't laugh...) What do I guess ( Please don't laugh ......)

> db.tests.find({$not : {deleted: true}})

(returns no results)( No results returned )

> db.tests.find({$not : {$eq:{deleted:true}}})

error: { "$err" : "invalid operator: $eq", "code" : 10068 } error :{“$ err”:“ Invalid operator :$ eq”,“code”:10068}

> db.tests.find({deleted:{$not: true}})

error: { "$err" : "invalid use of $not", "code" : 13041 } error :{“$ err”:“ Invalid use $ not”,“ Code ”:13041}

> db.tests.find({deleted:{$not: {$eq:true}}})

error: { "$err" : "invalid use of $not", "code" : 13034 } error :{“$ err”:“ Invalid use $ not”,“ Code ”:13034}

What am I missing? What I missed ?


Solution :

Reference resources : https://stackoom.com/en/question/1H2UQ
原网站

版权声明
本文为[Superior virtue and weak water]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141110208861.html