Saturday 8 April 2017

MongoDB Add custom field on insert OR JSON add custom key:value

Hya!

Long story short.

What I'm using:

node.js - mongodb - mongoose

Is there any way to add custom field to mongo collection on insert?

I insert some amount of data (~35Mb nothing serious) every 30 mins via JSON file and I would like to add a custom field which is static for every document that have been inserted at one time.

(actually this a timestamp which is already generated by another server, unfortunately mongodb already reserved "timestamps" for it's own inner usage so that's why I'm here)

My code looks something like these

...
var toomuchJSONdata = [{
    "name":"John",
    "age":30,
 }
 {
    "name":"Carl",
    "age":25,
}{
"....":"....",
"....":"....",

}]; ... to-my.collection.insertMany(toomuchJSONdata) ... So what's the best/easiest way to solve my problem? Or I'm moving in the wrong direction and it's much easier and better to add a custom "key":"value" for every JSON object? Like:

 var toomuchJSONdata = [{
 "name":"John",
 "age":30,
 "customkey":"customvalue"   
 }

What I have already done (..and still doing):

  • Experimented with .insert and mongoose.model
  • overlooked docs for mongoose/mongodb/native-mongodb-for node.js
  • I'm already google-coding for 3+ hours and still can't fix my problem.


via AlexZeDim

No comments:

Post a Comment