I am trying to insert multiple documents into a collection in mongoDB database, but am only getting only one object ID as response (multiple documents are getting created in the DB).
Code:
exports.createRelation = function(relationDoc, db, callback) {
var relations = db.get("relations");
relations
.insert( relationDoc )
.error( function ( err ) {
callback(err, null);
})
.success( function ( doc ){
callback(null, doc);
});
};
in this the relationDoc would be an array
Input:
newRelDocs_Array: [ {
path: [ 53d0b191c5ac61d403b0090d ]
tob: 1405343247 },
{
path: [ 53d0b191c5ac61d403b0090d ],
tob: 1405343247 } ]
Response :
createRelation(): Success
createRelation_db: {
_id: 546a1d6f65c05d1c37660c4c,
tob: 1405343247
}
FYI, I am using NodeJS, and connection to MongoDB with monk.
In your question you say realationDoc is an array which I presume looks something like this:
In monk, the insert function is not capable of the MonogoDB 2.4 built-in function Bulk Insert. See http://docs.mongodb.org/manual/reference/method/Bulk.insert/
With your code the way it is, you are creating a collection of a single document that has 2 fields, 1 the _id which is auto-generated and the second field that is your array.
To do what you want to do, you will need to modify your code like this:
Probably not as efficient as the Bulk Insert provided in the native driver, but should get you to where you need to go with monk.