美文网首页我爱编程
MongoDB CRUD操作

MongoDB CRUD操作

作者: 风雪围城 | 来源:发表于2017-06-07 22:42 被阅读0次

    CRUD操作即对文档进行create,read,update 和 update 。
    创建(create)操作
    创建或者插入都是添加一个新的文档(document)到集合(collection)中。如果集合不存在,插入操作将创建该集合。
    MongoDB提供下面的方法将一个文档插入到集合中:

    db.collection.insertOne({})
    db.collection.insertMany([{},{}])
    

    (在3.4.4版本中,如果一开始打开shell,并没有use某个数据库,会默认使用test数据库,如果该数据库不存在,则创建。在某个数据库下,插入时,如果没有该collection,则创建)。

    在MongoDB中,插入操作把集合作为目标。对于一个文档而言,所有的写入操作都是原子的(atomic)(An atomic operation in a database is an undividable and complex series of database operations such that either all operation will occur, or nothing will occur. The term atomicity is used to guarantee that there will be no partial database update )。

    插入操作

    读操作
    读操作就是从collection中索引document。MongoDB提供了下面的方法进行读取:

    db.collection.find()
    

    你可以指定选择器来确认要返回的document。

    查询操作

    更新操作
    更新操作就是编辑修改collection中已经存在的document。MongoDB提供了下面的方法来完成update操作:

    db.collection.updateOne()
    db.collection.updateMany()
    db.collection.replaceOne()
    

    在MongoDB中,更新操作的目标是一个单独的collection。所有的写操作在MongDB的document层级上是原子的。
    你可以指定标准或者过滤器来确认要update的document。

    更新操作

    删除操作
    删除操作就是从collection中删除document。MongoDB提供下面的方法完成删除:

    db.collection.deleteOne()
    db.collection.deleteMany()
    

    在MongoDB中,删除操作是针对一个单独的collection,所有的写入操作在MongoDB的document层级上,都是原子的。
    你可以指定标准,或者过滤器来确定要删除的document。

    删除操作

    相关文章

      网友评论

        本文标题:MongoDB CRUD操作

        本文链接:https://www.haomeiwen.com/subject/rrbafxtx.html