Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 801
Mark Avatar asked Feb 04 '11 20:02

Mark


People also ask

How fetch data from MongoDB by id in node JS?

connect('mongodb://localhost/Company'); var Schema = new mongoose. Schema({ _id : String, name: String, age : Number }); var user = mongoose. model('emp', Schema); app. param('id', function(req, res, next, id){ user.

How does the value of _ID get assigned to a document MongoDB?

In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field. This also applies to documents inserted through update operations with upsert: true.


1 Answers

var mongo = require('mongodb'); var o_id = new mongo.ObjectID(theidID); collection.update({'_id': o_id}); 
like image 139
KingPin Avatar answered Sep 19 '22 10:09

KingPin