MongoDB查询,其中不包括所有
我试图在MongDB中编写一个查询,以选择用户已部分完成其配置文件的所有行 其中f1、f2或f3为null或“”,但三个字段均为null或“” 我的意思是三个字段中的一个或两个是空的,但这三个字段不是空的 我尝试了此操作,但未返回预期记录: 已更新MongoDB查询,其中不包括所有,mongodb,mongoose,Mongodb,Mongoose,我试图在MongDB中编写一个查询,以选择用户已部分完成其配置文件的所有行 其中f1、f2或f3为null或“”,但三个字段均为null或“” 我的意思是三个字段中的一个或两个是空的,但这三个字段不是空的 我尝试了此操作,但未返回预期记录: 已更新 var query = { $or:[ {"profile.public.f1": {$in: [null, false, '']}}, {"profile.public.f2": {$in
var query = {
$or:[
{"profile.public.f1": {$in: [null, false, '']}},
{"profile.public.f2": {$in: [null, false, '']}},
{"profile.public.f3": {$in: [null, false, '']}}
]
};
此查询在NodeJ中使用,如下所示:
function GetDocument(model, query, projection, extension, callback) {
var query = db[model].find(query, projection, extension.options);
if (extension.populate) {
query.populate(extension.populate);
}
if (extension.sort) {
query.sort(extension.sort);
}
query.exec(function (err, docs) {
if (extension.count) {
query.count(function (err, docs) {
callback(err, docs);
});
} else {
callback(err, docs);
}
});
}
随后:
db.GetDocument('users', query, {}, {}, function (err, docdata) {
console.log(docdata)
});
使用$,其中:
> db.createCollection("t");
{ "ok" : 1 }
> db.t.insert({f1:'a',f2:'',f3:''});
WriteResult({ "nInserted" : 1 })
> db.t.insert({f1:'',f2:'',f3:null});
WriteResult({ "nInserted" : 1 })
> db.t.insert({f1:'',f2:'',f3:''});
WriteResult({ "nInserted" : 1 })
> db.t.insert({f1:'',f2:''});
WriteResult({ "nInserted" : 1 })
> db.t.find();
{ "_id" : ObjectId("58aab60118633becf0ac4f80"), "f1" : "a", "f2" : "", "f3" : "" }
{ "_id" : ObjectId("58aab60718633becf0ac4f81"), "f1" : "", "f2" : "", "f3" : null }
{ "_id" : ObjectId("58aab61018633becf0ac4f82"), "f1" : "", "f2" : "", "f3" : "" }
{ "_id" : ObjectId("58aab65618633becf0ac4f83"), "f1" : "", "f2" : "" }
> var q = {$where: function() {return ((this.f1.length||'') + (this.f2.length||'') +(this.f3 || '').length) >0;}};
> db.t.find(q);
{ "_id" : ObjectId("58aab60118633becf0ac4f80"), "f1" : "a", "f2" : "", "f3" : "" }
查询明细:
{
$where: function(){
return((this.f1.length||'')+(this.f2.length||'')+(this.f3||'').length)>0;
}
};
where should return true=>字符串总和(字段值)必须大于零。这样uf fieldN='',则其长度等于零,如果三者都等于零,则它们的和将为零(0+0+0)=0,但如果至少有一个值“”,则它将返回true,这样满足where子句
现在的问题是null.length不起作用,所以我们要么用字符串(null)将其包装起来,要么在失败时给出任何合适的值,因此我们得出了语义错误的结论,但是更短且有效的this.f1.length |'
如果第一个失败,第二个就起作用。操作员或拾取第一个工作值
如果您想在nodejs中使用它,而不是mongo shell中使用它,下面是一个示例:
vao@vao-VirtualBox:~$ nodejs
> var MongoClient = require('mongodb').MongoClient,
... co = require('co'),
... assert = require('assert');
undefined
>
> co(function*() {
... var db = yield MongoClient.connect('mongodb://localhost:27017/test');
... var col = db.collection('t');
... var q = {$where: function() {return ((this.f1.length||'') + (this.f2.length||'') +(this.f3 || '').length) >0;}};
... //var cursor = col.find({f1.length:{$gt:0}});
... var cursor = col.find({f1:"a"});
... col.find().toArray(function(err, items) {});
... var stream = col.find(q).stream();
... stream.on("data", function(item) {});
... stream.on("end", function() {});
...
... while(yield cursor.hasNext()) {
... var doc = yield cursor.next();
... console.dir(doc);
... }
...
... db.close();
... }).catch(function(err) {
... console.log(err.stack);
... });
Promise { <pending> }
>
> { _id:
ObjectID {
_bsontype: 'ObjectID',
id:
Buffer {
'0': 88,
'1': 172,
'2': 1,
'3': 143,
'4': 157,
'5': 175,
'6': 27,
'7': 230,
'8': 160,
'9': 208,
'10': 69,
'11': 88 } },
f1: 'a',
f2: '',
f3: '' }
(To exit, press ^C again or type .exit)
>
vao@vao-VirtualBox:~$nodejs
>var MongoClient=require('mongodb')。MongoClient,
... co=要求(“co”),
... assert=require('assert');
未定义
>
>文书主任(职能*(){
…var db=yield MongoClient.connect('mongodb://localhost:27017/test');
…var col=db.collection('t');
…var q={$where:function(){return((this.f1.length | |'')+(this.f2.length | |'')+(this.f3 | |'').length)>0;};
…//var cursor=col.find({f1.length:{$gt:0});
…var cursor=col.find({f1:“a”});
…col.find().toArray(函数(err,items){});
…var stream=col.find(q).stream();
关于(“数据”,函数(项){});
…stream.on(“end”,function(){});
...
…while(yield cursor.hasNext()){
…var doc=yield cursor.next();
…控制台目录(doc);
... }
...
…db.close();
…).catch(函数(err){
…控制台日志(错误堆栈);
... });
承诺{}
>
>{u id:
目标{
_bsontype:'ObjectID',
身份证件:
缓冲区{
'0': 88,
'1': 172,
'2': 1,
'3': 143,
'4': 157,
'5': 175,
'6': 27,
'7': 230,
'8': 160,
'9': 208,
'10': 69,
'11': 88 } },
f1:‘a’,
f2:“”,
f3:''}
(要退出,请再次按^C或键入.exit)
>
请您详细解释一下:将其分解:var q={$where:function(){return((this.f1.length | |“”)+(this.f2.length | |“”)+(this.f3 | | |“”)>0;}代码>更新的答案。很抱歉输入错误-家用笔记本电脑非常不舒服Vao Tsun感谢您的努力。这在nodejs行吗?Vao Tsun谢谢。这些字段类似于“profile.public.f2”、“profile.public.f2”和“profile.public.f3”这样的字段。这个profile.public.f2.length似乎不起作用。所有的记录都归还了,让我们来看看。