0

My json looks like this:

{
"att1":"Hello",
"att2": "world",
"att3": //a date object with now as value
 }

I want to use something like $currentDate at att3 with mongoimport.

Is it possible ?

bubbles
  • 2,597
  • 1
  • 15
  • 40

1 Answers1

0

Use this way

I suggest to create/use one file called like common.jsAnd put this code in that file.

getCurrentTimeStamp() {
    let date = new Date()
    return { date: date, timestamp: Math.floor((new Date()).getTime() / 1000) }
}

Now you can use above function , just import/use the common.js file.

As in your case, you may use it like this.

Import common file first.

 let data = {
   "att1":"Hello",
   "att2": "world",
   "att3": common.getCurrentTimeStamp().date,
   "att": common.getCurrentTimeStamp().timestamp  // FYI
 }

Now you have two options either you can use data or timestamp.

console.log("Date is ::: " , data.att3);
console.log("Timestamp is ::: " , data.att4);

MongoImport command

You may use it like this way.

mongoimport --db dbName --collection collectionName --file fileName.json --jsonArray

For more details

Update

db.users.update(
 { _id: 1 },
 {
  $currentDate: {         
     "date": { $type: "timestamp" }
  }
 }
)
Sachin Shah
  • 4,503
  • 3
  • 23
  • 50