因为这次项目中MongoDB的数据是字符串格式,我想执行一个计算过程,发现无论怎么改得到的结果都是null,后来发现MongoDB对字符串格式的数据是不能进行操作的。因为我也不能直接改数据库原表的结构,只能将需要的数据放入临时表来进行操作。

  话不多说,直接放代码:

第一种:将查询到的结果放入临时表temp_test中:

1     db.Collection.find({
2 //在这里可以放入查询条件
3 }).forEach(function(item) {
4         db.temp_test.insert(item)
5     })

第二种:直接将要用的数据insert到临时表temp中,可以一次插入多条数据:


 1   db.getCollection("temp").insert(
 2         [
 3             {
 4             "pollutant_code": "g71702",
 5             "cw_value": 2.03,
 6             "qc_value": 97.36
 7             },
 8             {
 9             "pollutant_code": "g71802",
10             "cw_value": 0.97,
11             "qc_value": 65.00
12             },
13             {
14             "pollutant_code": "g72202",
15             "cw_value": 276.33,
16             "qc_value": 99.99
17             }
18         ]
19     )
删除临时表:
1 db.temp_test.drop()
2 db.temp.drop()