mongodb增删改查之删

1,186次阅读次阅读
没有评论

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()
admin
版权声明:本站原创文章,由admin2018-05-17发表,共计2051字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)