Querying A Mongodb Based On Mongo Id In A Node.js App
I'm using a node.js and mongodb, and I'm trying to query the database based on the mongo generated ID using the following: collection.findOne( {_id:doc._id} , function(err, ite
Solution 1:
The MongoDb is an object not a string. To convert my string I used:
var id = require('mongodb').ObjectID(doc._id);
This converts my string into a mongo ObjectId and matches the _id in the db!
Solution 2:
Following is the example which spots the issue:
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db,
ObjectID = require('mongodb').ObjectID;
varMongoClient = require('mongodb').MongoClient//let id = your _id, smth like '6dg27sh2sdhsdhs72hsdfs2sfs'...var obj_id = newObjectID('52cbd028e9f43a090ca0c1af');
var justId = '52cbd028e9f43a090ca0c1af'; // <== This will not workMongoClient.connect('mongodb://127.0.0.1:27017/YourDbName', function(err, db) {
console.log('err' + err);
db.collection('YourCollectionName', function(error, collection) {
//collection.find({_id:justId}),function(err, docs) { // <== This will not work
collection.findOne({_id:obj_id},function(err, docs) {
console.log("Printing docs from Array. count " + JSON.stringify(docs));
});
});
});
Solution 3:
Use this:
ObjectId = require('mongodb').ObjectID;
Then when you try to find an object in collection by _id use this:
console.log("find by: "+ id);
database.collection("userRegister").findOne({_id: newObjectId(id)},
function(err, res) {
if (err) console.log(err);
if(res!=null){
console.log(res)
returnfalse;
}
if(res==null){
callback({'status':_error,'flag':'notexist','message':_userNotExist});
returnfalse;
}
});
Solution 4:
First we need to get ObjectID
from mongodb
library and need to create new instance in following way., so that you will get the ObjectID of string. If your are using es6 in your code this code
import { ObjectID } from'mongodb';
var emQuery = [
{
$match: {
_id: newObjectID(tlvaltResult[0].customers.createdBy)
}
},
{
$project: {
_id:1,
emailId:1,
mobile:1
}
}
];
console.log(emQuery,'emQuery');
[ { '$match': { _id: 5ad83ff0b443435298741d3b } },
{ '$project': { _id: 1, emailId: 1, mobile: 1 } } ]
var emResult = awaitUser.getAggregation(emQuery);
console.log(emResult,'emResult');
[ { _id: 5ad83ff0b443435298741d3b,
emailId: 'superAdmin@limitlessmobile.com' } ]
Solution 5:
First, ensure you've added all required modules in MongoDB config:
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db,
ObjectID = require('mongodb').ObjectID;
varBSON = require('mongodb').BSONPure;
var server = newServer('localhost', 27017, {
auto_reconnect: true
});
var db = newDb('YOUR_DB_NAME', server);
Then, when you try to find an object in collection by _id, use:
//let id = your _id, smth like '6dg27sh2sdhsdhs72hsdfs2sfs'...var obj_id = BSON.ObjectID.createFromHexString(id);
db.collection("NAME_OF_COLLECTION_WHERE_IS_YOUR_OBJECT", function(error, collection) {
collection.findOne( {_id:obj_id} , function(err, item) {
// console.log ( item.username );
});
});
Hope, this works.
Post a Comment for "Querying A Mongodb Based On Mongo Id In A Node.js App"