zoukankan      html  css  js  c++  java
  • mogodbshell中数组对象查询修改方法

    在mongodb中,存在如下数据

    { "_id" : ObjectId("59af55078a8fc5e51ff425de"), "title" : "title1", "col" : "col
    1", "reader" : [ { "readername" : "jim", "isread" : true }, { "readername" : "ka
    te" }, { "readername" : "lilei" } ], "begindate" : "Wed Sep 06 2017 09:53:11 GMT
    +0800 (中国标准时间)" }
    { "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col
    1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern
    ame" : "lilei" }, { "readername" : "lily" } ], "begindate" : "Wed Sep 06 2017 09
    :53:50 GMT+0800 (中国标准时间)" }
    { "_id" : ObjectId("59af55458a8fc5e51ff425e0"), "title" : "title3", "col" : "col
    1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" } ], "beginda
    te" : "Wed Sep 06 2017 09:54:13 GMT+0800 (中国标准时间)" }

    需求1:查询栏目是col1,且读者是lily的记录:

    > db.articles.find({col:'col1','reader.readername':'lily'})
    //查询结果
    { "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col
    1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern
    ame" : "lilei" }, { "readername" : "lily" } ], "begindate" : "Wed Sep 06 2017 09
    :53:50 GMT+0800 (中国标准时间)" }

    即数组中的对象用形如“数组名.字段”组成

    需求2:把标题为title2,且读者为lily的已读记录‘isread’设置为true

    > db.articles.update({title:'title2','reader.readername':'lily'},{$set:{'reader.
    $.isread':true}})
    
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

    { "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col 1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern ame" : "lilei" }, { "readername" : "lily", "isread" : true } ], "begindate" : "W ed Sep 06 2017 09:53:50 GMT+0800 (中国标准时间)" }

    核心是$,可以理解为数组定位器

    需求3:删除名为title2下的reader名字为lilei的读者对象:

    //未删除前数据
    { "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col
    1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern
    ame" : "lilei" }, { "readername" : "lily", "isread" : true } ], "begindate" : "W
    ed Sep 06 2017 09:53:50 GMT+0800 (中国标准时间)" }
    
    //执行命令
    > db.articles.update({title:'title2','reader.readername':'lilei'},{$pull:{'reader':{readername:'lilei'}}})
    
    //执行命令后查询的结果
    > db.articles.find({title:'title2'});
    { "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col
    1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern
    ame" : "lily", "isread" : true } ], "begindate" : "Wed Sep 06 2017 09:53:50 GMT+
    0800 (中国标准时间)" }

    核心是用$pull命令查找数组名称,然后通过属性值删除数组内的对象记录

  • 相关阅读:
    (笔记)Linux内核学习(二)之进程
    (笔记)Linux内核学习(一)之内核介绍
    状态机思路在程序设计中的应用
    内存操作函数memmove,memcpy,memset
    linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime
    Camera ISO、快门、光圈、曝光这几个概念
    C语言中的指针和内存泄漏几种情况
    音视频文件码率与大小计算
    CC++中 fopen中文件打开方式的区别:
    常用DOS命令
  • 原文地址:https://www.cnblogs.com/qkabcd/p/7483357.html
Copyright © 2011-2022 走看看