Skip to content

Instantly share code, notes, and snippets.

@dineshsprabu
Last active December 30, 2015 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dineshsprabu/94dda75f27723043ebd1 to your computer and use it in GitHub Desktop.
Save dineshsprabu/94dda75f27723043ebd1 to your computer and use it in GitHub Desktop.
Setting _id as raw String on Mongo from NodeJS (Solution: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters)
/* The unwanted complication on monk ('_id' as HEX 12-24 bytes) can be handled by using native mongo driver */
var MongoClient = require('mongodb').MongoClient;
/* values to be changed as per need */
var input = {'_id':'string_value'};
var collection_name = 'collection_name';
var db_name = 'db_name';
MongoClient.connect("mongodb://localhost:27017/"+db_name, function(err, db) {
if(err) { return console.dir(err); }
var collection = db.collection(collection_name);
collection.insert(input);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment