Search code examples
node.jsmongodbmonk

Monk db.find not working


I am trying to grab an ObjectId() out of one of my mongodb fields. The problem is that I know what I am trying to grab is in the db but says it can not be found. I have already verified token that I am looking for in the db is working.

Code:

var _db = db.get('tokens');
  console.log('token ' + token);
  //token = 587b09875d690d2f4f11849c
  _db.find({'token': token}, function(e, docs){
  console.log('Docs ' + docs);
  if(!e){
    if(docs.length > 0){
      if(expire < phpTime()){
        cb(true);
      }else{
        console.log('Token has expired');
        cb(false);
      }
    }else{
      cb(false);
      console.log('Token not found in db');
    }
  }else{
    cb(false);
    console.log(e);
  }
});

In the db I have:

{
"_id": ObjectId('587b09875d690d2f4f11849d'),
"token": ObjectId('587b09875d690d2f4f11849c'),
"expire": 0
}

I keep getting the console.log('Token not found in db');. I am really lost on why it is in the db, the token it is looking for are the same but _db.find can not find it.


Solution

  • I found the solution, apparently it was not working right when I had an ObjectId() in one of my fields I was referencing (token). So I used the code ObjectId().toHexString() and stored that into the db so it looked like 5824b9f7611202236ec99c7e instead of looking like ObjectId("5824b9f7611202236ec99c7e"). Hope this helps!