开发者

Is it possible to do an increment with mongoimport?

开发者 https://www.devze.com 2023-03-30 08:01 出处:网络
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)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消