mongoDB--update和delete

mongoDB提供的update和delete方法中有对collection中一个document操作的(类似于limit 1),也有对collection中多个doucument操作的,还支持blukUpdate(这个有点像hibernateTemplate或是jdbcTemplate提供的blukUpdate)

UPDATE OPERATION:

  1.updateOne

  db.class.updateOne({

      filterProp:{$lt:filterVal}

    },{

      $set : {setProp:setVal}    

   })

对等

update class set setProp=setVal

where filterProp < filterVal

limit 1

只是修改命中的一条数据的update的信息

 好吧,是我错了,我的版本不支持updateOne,只有update

  2.updateMany

   db.class.updateMany({

      filterProp:{$lt:filterVal}

    },{

      $set : {setProp:setVal}    

   })

对等

update class set setProp=setVal

where filterProp < filterVal

  3.replaceOne(我这里实验的是较低版本的mongoDB)

replaceOne其实和我贴图中的update是一样的,和updateOne不同的是:

  updateOne是对document中的某个prop替换value值

  replaceOne是将满足的document替换掉用后面的collection



 

DELETE OPERATION:

 1.deleteOne

以下是摘抄from API

 The following diagram highlights the components of the MongoDB deleteOne() operation:

The components of a MongoDB deleteOne operation.

The following diagram shows the same query in SQL:

The components of a SQL DELETE statement with LIMIT 1.

 2.deleteMany


The following diagram highlights the components of the MongoDB deleteMany() operation:

The components of a MongoDB deleteMany operation.

The following diagram shows the same query in SQL:

The components of a SQL DELETE statement.

 3.remove



 

remove 和deleteOne的区别:

暂时没有发现有什么区别。。。。//TODO待补充

bulkWrite:(3.2版本新函数)

是将对应的update行为,组成jsonArray一次性起效。

db.collection.bulkWrite(
   [
      { insertOne : { "document" : { name : "sue", age : 26 } } },
      { insertOne : { "document" : { name : "joe", age : 24 } } },
      { insertOne : { "document" : { name : "ann", age : 25 } } },
      { insertOne : { "document" : { name : "bob", age : 27 } } },
      { updateMany: {
         "filter" : { age : { $gt : 25} },
         "update" : { $set : { "status" : "enrolled" } }
         }
      },
      { deleteMany : { "filter" : { "status" : { $exists : true } } } }
   ]
)

   


 

猜你喜欢

转载自itwork4liu.iteye.com/blog/2298268