I am trying to import data in JSON format into a MongoDB collection using mongoimport and files saved on disk. This all works perfectly fine but I need to increment a specific field ("hits" by one) whenever there are duplicates possibly using the $inc operator. How do I do that? Is it possible?
A sample record from the file looks like this:
{"date":"2011","loc":{"lon":"-95.3436","lat":"29.0335"},"hits":1}
My import statement is as follows:
mongoimport --host localhost --db tst --collection year --file y.json --upsert --upsertFields date,loc
I have tried with/out "--upsert" / "--upsertFields" which does not help here.
Any advice is highly appreci开发者_Go百科ated!
No, that isn't possible with mongoimport, it'll only import whatever data is passed in. You could write a script to parse your JSON files and make the modifications before importing; or import it into Mongo then build a query to modify the data once it's in the database.
精彩评论