繁体   English   中英

无法使用NodeJS中的find查询mongodb

[英]Unable to query mongodb with find in NodeJS

我有一个使用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("..."))
        }
}

我使用“ 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));
                                            }
                                        }
                                    });

该代码不起作用,我总是得到“未找到”答案,但是使用mongo客户端CMD我找到了一个对我有用的代码:

db.service.find({"date": ISODate("..."), "codeCh": "...", "codeConv": "...", "appareil._id":  ObjectId("..."), "bus._id" :  ObjectId("...")}).pretty()

该查找查询的目的是确保服务集合中5个字段的组合的唯一性,我尝试添加如下所示的复合索引:

db.service.ensureIndex( {date:1, appareil:1, bus:1, codeCh:1, codeConv:1}, { unique: true, dropDups: true } )

但这也行不通...

如何仅使用这5个字段来处理查找查询,或者如何使这些字段的组合在集合设置中唯一?

如果看到您的代码。 你有console.log('not found'); else情况下。 因此,只要没有错误,就not foundconsole打印出来。 我认为您的代码运行正常。

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
        }
    });

你应该做console.log('not found'); 同时检查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.

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