[英]Unable to query mongodb with find in NodeJS
I have a MongoDB database that I am querying using NodeJS, I have a collection "service" with 2 fields containing a dbref to another 2 collections ("appareil" & "bus"), so typically a position doc is similar to this: 我有一个使用NodeJS查询的MongoDB数据库,我有一个“服务”集合,其中包含2个字段,其中包含指向另外2个集合(“ appareil”和“ bus”)的dbref,因此通常情况下,位置文档与此类似:
{
"_id" : ObjectId("..."),
"idService" : ...,
"codeRoute" : ...,
"codeCh" : "...",
"codeConv" : "...",
"codeLigne" : "...",
"date" : ISODate("..."),
"appareil" : {
"_id" : ObjectId("..."),
"_class" : "com.transtu.documents.Appareil",
"reference" : "...",
"societe" : DBRef("societe", ObjectId("..."))
},
"bus" : {
"_id" : ObjectId("..."),
"_class" : "com.transtu.documents.Bus",
"code" : "...",
"immatriculation" : "...",
"reference" : "...",
"localisation" : {
"x" : ...,
"y" : ...
},
"societe" : DBRef("societe", ObjectId("..."))
}
}
I tried the following code in NodeJs using the "mongodb" npm module : 我使用“ mongodb” npm模块在NodeJs中尝试了以下代码:
findService = {};
findService.date = 'ISODate(' + new Date(service.date).toISOString() + ')';
findService.appareil = {_id: 'new ObjectID(' + service.appareil._id + ')'};
findService.bus = {_id: 'new ObjectID(' + service.bus._id + ')'};
findService.codeCh = service.codeCh;
findService.codeConv = service.codeConv;
serviceCollection.find(findService).toArray(function (err, docs) {
if (err) {
console.error('error:............... ' + JSON.stringify(err));
} else {
//console.log('docs:............ ' + JSON.stringify(docs));
if (docs.length == 0) {
console.log('not found'); serviceCollection.insertOne(service, function (res, err) {
if (err) {
//console.error('err: ............: ' + JSON.stringify(err));
} else {
//console.log('res: **************: ' + JSON.stringify(res));
}
});
} else {
console.log('found');
console.log('docs: ****************************** ' + JSON.stringify(docs));
}
}
});
This code is not working and I always get "not found" answer, but using the mongo client CMD I found a code that worked for me: 该代码不起作用,我总是得到“未找到”答案,但是使用mongo客户端CMD我找到了一个对我有用的代码:
db.service.find({"date": ISODate("..."), "codeCh": "...", "codeConv": "...", "appareil._id": ObjectId("..."), "bus._id" : ObjectId("...")}).pretty()
The goal from this find query is to ensure the uniqueness of a combination of 5 fields in the service collection, I tried adding a compound index like this: 该查找查询的目的是确保服务集合中5个字段的组合的唯一性,我尝试添加如下所示的复合索引:
db.service.ensureIndex( {date:1, appareil:1, bus:1, codeCh:1, codeConv:1}, { unique: true, dropDups: true } )
but it didn't work too... 但这也行不通...
How can I process a find query with only these 5 fields or how to make the combination of these fields unique in the collection settings? 如何仅使用这5个字段来处理查找查询,或者如何使这些字段的组合在集合设置中唯一?
If you see your code. 如果看到您的代码。 you have
console.log('not found');
你有
console.log('not found');
in your else
condition. 在
else
情况下。 So, whenever there is no error, you are getting not found
printed in the console
. 因此,只要没有错误,就
not found
在console
打印出来。 I think your code is working fine. 我认为您的代码运行正常。
serviceCollection.find(findService)
.toArray(function (err, docs) {
if (err) {
console.error('error:............... ' + JSON.stringify(err));
} else {
console.log('not found');//this will print "not found" if there is no error.
//rest of the code
}
});
you should do console.log('not found');
你应该做
console.log('not found');
while checking the length of docs
. 同时检查
docs
的长度。
serviceCollection.find(findService)
.toArray(function (err, docs) {
if (err) {
console.error('error:............... ' + JSON.stringify(err));
} else {
if (docs.length == 0) {
console.log('not found'); //this should be here
serviceCollection.insertOne(service, function (res, err) {
if (err) {
//console.error('err: ............: ' + JSON.stringify(err));
} else {
//console.log('res: **************: ' + JSON.stringify(res));
}
});
} else {
console.log('found');
console.log('docs: ****************************** ' + JSON.stringify(docs));
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.