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?
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.
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.
var mongo = require('mongodb'); var o_id = new mongo.ObjectID(theidID); collection.update({'_id': o_id});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With