简体   繁体   English

Mongodb查找不适用于Objectid

[英]Mongodb find is not working with the Objectid

Here is my code and I'm getting null value for the doc. 这是我的代码,并且该文档的值为空。 But in the findOne({'webpageid':docs[i]._id} instead of docs[i]._id if I pass the webpageid it works. And I verified docs has the required webpage data 但是在findOne({'webpageid':docs [i] ._ id}而不是docs [i] ._ id中,如果我通过了webpageid,它就可以工作。并且我验证了docs是否具有必需的网页数据

 // get all the records from the webpages table var collection = db.get('webpages'); collection.find({}, function(e,docs){ // iterate over the webpage list for (var i = 0; i < docs.length; i++) { // check if results table has any record for this webpage db.get('results').findOne({'webpageid':docs[i]._id}, function(err, doc) { if(err) { return; } console.log(doc); }); }; }); 

Below is the content in the 'webpages' table 以下是“网页”表中的内容

 { "_id" : ObjectId("549608e16ecb16dc3c4880e6"), "name" : null, "url" : "http://www.google.com", "texttoverify" : null, "interval" : null, "websiteid" : null } { "_id" : ObjectId("549609986ecb16dc3c4880e7"), "name" : null, "url" : "http://www.google.com", "texttoverify" : null, "interval" : null, "websiteid" : null } { "_id" : ObjectId("54960a916ecb16dc3c4880e8"), "name" : "xyz", "url" : "http://www.google.com", "texttoverify" : "hello", "interval" : "00:15:00.0000000", "websiteid" : "02D7BE81-4E01-4347-BAE5-BA9A2D7EE11E" } 

Here is my content inside 'results' table 这是我的“结果”表中的内容

 { "_id" : ObjectId("54985a1e69af082f9c477574"), "webpageid" : "549608e16ecb16dc3c4880e6" } { "_id" : ObjectId("54986f4e69af082f9c477575"), "name" : "name" } { "_id" : ObjectId("5498d10c69af082f9c477576"), "name" : "name", "webpageid" : "\\"54960a916ecb16dc3c4880e8" } { "_id" : ObjectId("5498d1f969af082f9c477577"), "name" : "name", "webpageid" : "54960a916ecb16dc3c4880e8" } 

Thanks for looking into it and appreciate your help. 感谢您调查并感谢您的帮助。

Your webpageids are strings while the _ids that you are searching with are ObjectIds. 您的网页ID是字符串,而用于搜索的_id是ObjectId。 Your find does not match any results because there are no documents in the results table which have ObjectId values for the "webpageid" element. 您的发现与任何结果都不匹配,因为结果表中没有文档包含“ webpageid”元素的ObjectId值。

There are two solutions as I see it. 我认为有两种解决方案。

  1. You could store ObjectIds instead of strings for the webpageid element in the results collection. 您可以在results集中存储ObjectIds而不是webpageid元素的字符串。 The implementation of this is of course based on how those documents get into the collection. 当然,此实现基于这些文档如何进入集合。
  2. You could create a string variable from the returned ObjectId within the loop to compare instead. 您可以从循环中返回的ObjectId创建一个字符串变量来进行比较。 For example, 例如,

     ... for(var i = 0; i < docs.length; i++) { var docId = docs[i]._id.toString(); // create a string db.get('results').findOne({'webpageid':docId}, function(err, doc) ... 

If you go with the second option, you may need to look into why there is a leading quote at the beginning of the webpageid value for the second document in the results collection. 如果您选择第二个选项,则可能需要研究为什么results集合中第二个文档的webpageid值开头有引号。

"webpageid" : "\"54960a916ecb16dc3c4880e8"

Lastly, I don't know much about your requirements, but you may want to rethink MongoDB as a solution. 最后,我对您的要求了解不多,但是您可能需要重新考虑MongoDB作为解决方案。 It appears that you are creating something like a JOIN which is something MongoDB is not designed to handle very well. 看来您正在创建类似JOIN之类的东西,而MongoDB并非旨在将其设计得很好。

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

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