Cherry g
Cherry g

Reputation: 276

Mongo db returning empty results

My code:

MongoClient.connect(gloabl_vars.db.mongo.url,function(err, db) {
        if(err) { throw err;    }
        var dbo=db.db("profilemanager");      
         var mquery={_id:'123454'}; 
 db.collection('userinfo').find(mquery,{'_id':0,'subscriptions':1}).toArray(function(err,result){
          if(err) throw err;
          console.log(result); 
        });
      });
      }

am able to get the result from Robo3T mongo client but same is returning null through nodejs.

Robo3T:
    db.getCollection('userinfo').find({_id:'66613'},{'_id':0,'subscriptions':1});

Upvotes: 0

Views: 167

Answers (1)

Milad Aghamohammadi
Milad Aghamohammadi

Reputation: 1966

You are searching a record by {_id:'66613'} in Robo3T but your sample is {_id:'123454'} in node.js. Also projection in node.js find is not in this way. Try below Snippet

MongoClient.connect(gloabl_vars.db.mongo.url,function(err, db) {
        if(err) { throw err;    }
        var dbo=db.db("profilemanager");      
        var mquery={_id:'66613'}; 
db.collection('userinfo').find(mquery).project({'_id':0,'subscriptions':1}).toArray(function(err,result){
        if(err) throw err;
        console.log(result); 
        });
    });
    }

Upvotes: 1

Related Questions