1

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

// 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.

Pritam Karmakar
  • 2,773
  • 5
  • 30
  • 49
  • you need to extract the HexaDecimal String value of the ObjectId, before using it to query the results collection. - http://stackoverflow.com/questions/13104690/nodejs-mongodb-object-id-to-string, since the `results` collection has `webpageid` as string and not an `ObjectId`. – BatScream Dec 29 '14 at 21:18

1 Answers1

1

Your webpageids are strings while the _ids that you are searching with are ObjectIds. Your find does not match any results because there are no documents in the results table which have ObjectId values for the "webpageid" element.

There are two solutions as I see it.

  1. You could store ObjectIds instead of strings for the webpageid element in the results collection. 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. 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.

"webpageid" : "\"54960a916ecb16dc3c4880e8"

Lastly, I don't know much about your requirements, but you may want to rethink MongoDB as a solution. It appears that you are creating something like a JOIN which is something MongoDB is not designed to handle very well.

Pritam Karmakar
  • 2,773
  • 5
  • 30
  • 49
NoOutlet
  • 1,949
  • 1
  • 14
  • 22
  • Can you include in your answer what the OP needs to do in order to make this work. That will complete this answer. – BatScream Dec 29 '14 at 21:22
  • Thanks for your help. Really appreciate it. I think the problem is with objectid vs string. Here is (https://www.dropbox.com/s/1r0fkafs8l48b6t/mongodb.txt?dl=0) what I tried but still getting null. I'm using mongojs as driver https://github.com/mafintosh/mongojs – Pritam Karmakar Dec 29 '14 at 22:19
  • @NoOutlet: we can ignore that leading quote record ("webpageid" : "\"54960a916ecb16dc3c4880e8"). That I added by mistake. – Pritam Karmakar Dec 29 '14 at 22:24
  • Wooho..it worked thanks a lot guys. I made a small edit in your code - replaced 'str' with toString(). Here is my final code if anyone interested https://www.dropbox.com/s/wq9cyqblxg66w6b/mongodb_final.txt?dl=0 – Pritam Karmakar Dec 29 '14 at 22:34
  • 1
    In the shell you could also use: `_id.valueOf()` function. – BatScream Dec 29 '14 at 22:52