MongoDB db.collection.remove()方法

mongodb中删除document采用remove方法,

http://docs.mongodb.org/manual/reference/method/db.collection.remove/

dbDao 百度贴吧:http://tieba.baidu.com/dbdao

MongoDB技术学习QQ群: 421431253

db.collection.remove(),从一个collection中移除对应的document。db.collection.remove()可以有2种用法:

 

 

db.collection.remove(
 <query>,
 <justOne>
)

justone是布尔类型,是可选的参数,来保证只删除一个document; 默认为false即删除所有符合要求的document>

从版本2.6开始加入了新的语法:

db.collection.remove(
   ,
   {
     justOne: ,
     writeConcern: 
   }
)

 

writeConcern 关于safe write。

下面的是db.collection.remove()的使用例子

 

其中new Date方法用来返回一个mongoldb中的ISOdate类型

 

 

ac:~ maclean$ mongo
MongoDB shell version: 3.0.2
connecting to: test
> 
> 
> db.dbdao_email.insert({email_title:"dbdao email!",date: new Date("1999-01-01")});
WriteResult({ "nInserted" : 1 })
> 
> 
> 
> 
> db.dbdao_email.find();
{ "_id" : ObjectId("554766d2e90479237b67c782"), "email_title" : "dbdao email!", "date" : ISODate("1999-01-01T00:00:00Z") }
> db.dbdao_email.insert({email_title:"dbdao email!",date: new Date("1999-02-01")});
WriteResult({ "nInserted" : 1 })
> db.dbdao_email.insert({email_title:"dbdao email!",date: new Date("1999-02-01")});
WriteResult({ "nInserted" : 1 })
> db.dbdao_email.insert({email_title:"dbdao email!",date: new Date("1999-02-01")});
WriteResult({ "nInserted" : 1 })
> db.dbdao_email.insert({email_title:"dbdao email!",date: new Date("2001-02-01")});
WriteResult({ "nInserted" : 1 })
> db.dbdao_email.insert({email_title:"dbdao email!",date: new Date("2002-02-01")});
WriteResult({ "nInserted" : 1 })
> 
> 
> db.dbdao_email.find({date:{ $lt : new Date("2000-01-01")}});
{ "_id" : ObjectId("554766d2e90479237b67c782"), "email_title" : "dbdao email!", "date" : ISODate("1999-01-01T00:00:00Z") }
{ "_id" : ObjectId("554766e4e90479237b67c783"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766e5e90479237b67c784"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766e6e90479237b67c785"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
> 
> db.dbdao_email.find();
{ "_id" : ObjectId("554766d2e90479237b67c782"), "email_title" : "dbdao email!", "date" : ISODate("1999-01-01T00:00:00Z") }
{ "_id" : ObjectId("554766e4e90479237b67c783"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766e5e90479237b67c784"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766e6e90479237b67c785"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766ece90479237b67c786"), "email_title" : "dbdao email!", "date" : ISODate("2001-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766efe90479237b67c787"), "email_title" : "dbdao email!", "date" : ISODate("2002-02-01T00:00:00Z") }
> 
> db.dbdao_email.find({date:{ $lt : new Date("2000-01-01")}});
{ "_id" : ObjectId("554766d2e90479237b67c782"), "email_title" : "dbdao email!", "date" : ISODate("1999-01-01T00:00:00Z") }
{ "_id" : ObjectId("554766e4e90479237b67c783"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766e5e90479237b67c784"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766e6e90479237b67c785"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
> 
> db.dbdao_email.remove({date:{ $lt : new Date("2000-01-01")}})
WriteResult({ "nRemoved" : 4 })
> db.dbdao_email.find({date:{ $lt : new Date("2000-01-01")}});
> db.dbdao_email.find();
{ "_id" : ObjectId("554766ece90479237b67c786"), "email_title" : "dbdao email!", "date" : ISODate("2001-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766efe90479237b67c787"), "email_title" : "dbdao email!", "date" : ISODate("2002-02-01T00:00:00Z") }

有的时候为了减少存储使用,用户决定删除collection中的部分记录,则可以用db.emails.remove({“date”: {“lt”: new Date(“2001-01-01”)}})

Comment

*

沪ICP备14014813号

沪公网安备 31010802001379号