Mark Mark - 3 months ago 13
Node.js Question

node.js mongodb select document by _id node-mongodb-native

I'm trying to select a document by id

I've tried:

collection.update({ "_id": { "$oid": + theidID } }

collection.update({ "_id": theidID }

collection.update({ "_id.$oid": theidID }}


Also tried:

collection.update({ _id: new ObjectID(theidID ) }


This gives me an error 500...

var mongo = require('mongodb')
var BSON = mongo.BSONPure;
var o_id = new BSON.ObjectID(theidID );

collection.update({ _id: o_id }


None of these work. How to select by _id?

Answer
var mongo = require('mongodb');
var o_id = new mongo.ObjectID(theidID);
collection.update({'_id': o_id});
Comments