mongodb 语句汇总

2018-03-15 15:57:19

1. 修改数据类型-String转Int32

db.getCollection('eb_category').find({}).forEach(function (x) {
    x.pro_categoryid = NumberInt(x.pro_categoryid);
    db.getCollection('eb_category').save(x);
});

2.修改数据类型-Array转Object

db.getCollection('eb_product').find({'productid.0':{$exists:true}}).forEach(function(x){
  var c = {};
  x.productid.forEach(function(a){
   c[a] = {};
   c[a].productid = NumberInt(a);
   c[a].type = NumberInt(1);
   c[a].created_at = new Date()
  });
  x.productid = c;
  db.getCollection('eb_product').save(x);
})

3.去字段内空格

db.getCollection('pro_product').find({brand168id:{$regex:/\s/}}).forEach(function (x) {
    x.brand168id = x.brand168id.trim();
    db.getCollection('pro_product').save(x);
});

4.添加删除字段

db.getCollection('eb_shop').update({},{$set:{'logo':0}},true, true)
db.getCollection('eb_shop').update({},{$unset:{'isdel':''}},false, true)