I’m using mongoose to operate mongodb. Now, for testing, I want to inserting some data into mongodb by native connection.
But the question is how to get the generated id after inserting?
I tried:
var mongoose = require('mongoose');
mongoose.connect('mongo://localhost/shuzu_test');
var conn = mongoose.connection;
var user = {
a: 'abc'
};
conn.collection('aaa').insert(user);
console.log('User:');
console.log(user);
But it prints:
{ a: 'abc' }
There is no _id field.
You can generate
_idyourself and send it to the database.This is one of my favourite features of MongoDB. If you need to create a number of objects, that are linked to each other, you don’t need to make numerous round-trips between app and db. You can generate all ids in the app and then just insert everything.