MongoDB入门---文档操作之增删改

    之前的两篇文章,已经分享过关于MongoDB的集合还有数据库的各种操作,接下来就涉及到最主要的喽,那就是数据方面的操作,在这里叫做文档操作。话不多说,大家来看正文。

    首先来看一下它的数据结构:

    文档的数据结构和JSON基本一样。

    所有存储在集合中的数据都是BSON格式。

    BSON是一种类json的一种二进制形式的存储格式,简称Binary JSON。

    完事之后呢,咱们就来看一下MongoDB的基本语法,首先是插入:

    

db.COLLECTION_NAME.insert(document)

    好,来看一下实例感受一下效果:

    

>db.luyaran.insert({title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: 'luyaran', url: 'http://www.luyaran.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 })

    执行上述操作,插入就算完成了,大家可以通过查询来看一下:

> db.luyaran.find() { "_id" : ObjectId("56064886ade2f21f36b03134"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } >

   咱们呢也可以把数据定义为一个变量,如下:

> document=({title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: 'luyaran', url: 'http://www.luyaran.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 });

    然后直接执行,结果如下:

{
        "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }

        再来执行插入操作:

> db.luyaran.insert(document) WriteResult({ "nInserted" : 1 }) >

    到这里,插入操作就算是完成了,接下来就是更新操作了,也就是修改,看一下语法:

db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } )

   

下面是参数的一个说明:

  • query : update的查询条件,类似sql update查询内where后面的。
  • update : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
  • upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
  • multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
  • writeConcern :可选,抛出异常的级别。

   来,看一下实例感受下哈:

>db.luyaran.insert({ title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: 'luyaran', url: 'http://www.luyaran.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 })

    插入操作完成后,我们来试着修改一下哈:

>db.luyaran.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) # 输出信息 > db.luyaran.find().pretty() { "_id" : ObjectId("56064f89ade2f21f36b03136"), "title" : "MongoDB", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } >

    通过实例,大家可以看到,结果集以及修改过了。但是这种方式只能修改查询操作中发现的第一条,如果你要修改多条数据的话,就需要把multi参数修改为true。看一下实例:

>db.luyaran.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true})

    还有一种方式可以修改数据,那就是save操作,看一下语法:

db.collection.save( <document>, { writeConcern: <document> } )

   

这是一些参数说明哈:

  • document : 文档数据。
  • writeConcern :可选,抛出异常的级别。

    完事呢就来看一下实例:

>db.luyaran.save({     "_id" : ObjectId("56064f89ade2f21f36b03136"), "title" : "MongoDB", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "mongodb", "NoSQL" ], "likes" : 110 })

    执行上述代码成功后,大家可以来看一下结果集:

>db.col.find().pretty() { "_id" : ObjectId("56064f89ade2f21f36b03136"), "title" : "MongoDB", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "Runoob", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "NoSQL" ], "likes" : 110 } >

    注意哈:上面代码修改的是第一次插入所产生的数据,不是第一次修改所产生的数据哈。。。

还有更多的方式可以修改数据,本人在这里就直接复制粘贴下来了哈,就不一一赘述了:

只更新第一条记录:

db.col.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );

全部更新:

db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );

只添加第一条:

db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );

全部添加加进去:

db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );

全部更新:

db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );

只更新第一条记录:

db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );

    好啦,接下来就是删除了,来看一下语法格式哈:

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

        如果你的MongoDB的版本是2.6以后的,那么你的删除语法如下:

db.collection.remove( <query>, { justOne: <boolean>, writeConcern: <document> } )

   

这是一部分参数说明:

  • query :(可选)删除的文档的条件。
  • justOne : (可选)如果设为 true 或 1,则只删除一个文档。
  • writeConcern :(可选)抛出异常的级别。

    还是老样子哦,看实例说话:

>db.luyaran.insert({title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: 'luyaran', url: 'http://www.luyaran.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 })

    上述代码我们执行两次,插入luyaran表中两条数据。我们来查看一下:

> db.col.find() { "_id" : ObjectId("56066169ade2f21f36b03137"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5606616dade2f21f36b03138"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "luyaran", "url" : "http://www.luyaran.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }

    接下来,我们开始进行移除title为MongoDB 教程的数据:

>db.col.remove({'title':'MongoDB 教程'}) WriteResult({ "nRemoved" : 2 }) # 删除了两条数据 >db.col.find() …… # 没有数据

    这是全部删除了,如果你想要只移除第一次找到的一条的话:

>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)

    还有就是全表删除:

>db.col.remove({}) >db.col.find() >

    这就算是完事了,接下来就是重中之重喽,不过貌似是篇幅不够了,那就下次吧。。。

    先声明哦,本人自学笔记,各位看过勿喷吼。。。

    如果觉得不错,大家可以多多点赞关注支持一下哦。。。

   原文链接:https://blog.csdn.net/luyaran/article/details/79696785

猜你喜欢

转载自www.cnblogs.com/luyaran/p/8951840.html
今日推荐