mongodb增删改查之删

2,814次阅读
没有评论

共计 1124 个字符,预计需要花费 3 分钟才能阅读完成。

mongodb增删改查之删

deleteOne

删除符合条件的一条数据

{ 
    "_id" : ObjectId("5afd209e2c63ae1a943bdb20"), 
    "bar" : "test"
}
{ 
    "_id" : ObjectId("5afd21502c63ae1a943bdb21"), 
    "a" : "b"
}
{ 
    "_id" : ObjectId("5afd21502c63ae1a943bdb22"), 
    "c" : "d"
}
{ 
    "_id" : ObjectId("5afd25062c63ae1a943bdb23"), 
    "bar" : "test"
}
{ 
    "_id" : ObjectId("5afd254e2c63ae1a943bdb24"), 
    "bar" : "test"
}

结果

db.test.deleteOne({"bar":"test"})

{ 
    "_id" : ObjectId("5afd21502c63ae1a943bdb21"), 
    "a" : "b"
}
{ 
    "_id" : ObjectId("5afd21502c63ae1a943bdb22"), 
    "c" : "d"
}
{ 
    "_id" : ObjectId("5afd25062c63ae1a943bdb23"), 
    "bar" : "test"
}
{ 
    "_id" : ObjectId("5afd254e2c63ae1a943bdb24"), 
    "bar" : "test"
}

deleteMany

输出符合条件的多条数据

db.test.deleteMany({"bar":"test"})
{ 
    "_id" : ObjectId("5afd21502c63ae1a943bdb21"), 
    "a" : "b"
}
{ 
    "_id" : ObjectId("5afd21502c63ae1a943bdb22"), 
    "c" : "d"
}

remove

删除符合条件的多条数据,没发现和deleteMany之间的区别有知道的麻烦评论

原始数据
{ 
    "_id" : ObjectId("5afd21502c63ae1a943bdb21"), 
    "a" : "b"
}
{ 
    "_id" : ObjectId("5afd21502c63ae1a943bdb22"), 
    "c" : "d"
}
{ 
    "_id" : ObjectId("5afd2ac32c63ae1a943bdb25"), 
    "a" : "b"
}
{ 
    "_id" : ObjectId("5afd2ac32c63ae1a943bdb26"), 
    "c" : "d"
}
db.test.remove({"a":"b"})
{ 
    "_id" : ObjectId("5afd21502c63ae1a943bdb22"), 
    "c" : "d"
}
{ 
    "_id" : ObjectId("5afd2ac32c63ae1a943bdb26"), 
    "c" : "d"
}

drop

直接删除整个表数据,速度快

db.test.drop()
正文完
请博主喝杯咖啡吧!
post-qrcode
 
admin
版权声明:本站原创文章,由 admin 2018-05-17发表,共计1124字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码