MongoDB--使用修改器修改文檔

來源:互聯網
上載者:User

標籤:err   for   log   value   類型   rem   not   upd   添加   

     可以使用修改器啦修改文檔,比如增加、刪除文檔的索引值。使用修改器首先要定位到某個文檔, 然後再增加相應的修改選項,需要使用update語句

1.$inc修改器修改文檔

> db.users.findOne({‘name‘:‘cd‘});{        "_id" : ObjectId("584eafa97629396db95535da"),        "name" : "cd",        "sex" : "M",        "information" : {                "age" : 23,                "address" : "Shanghai"        }}> db.users.update({"name": "cd"},...    {"$inc":{"qq": 123456789}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.findOne({‘name‘:‘cd‘});{        "_id" : ObjectId("584eafa97629396db95535da"),        "name" : "cd",        "sex" : "M",        "information" : {                "age" : 23,                "address" : "Shanghai"        },        "qq" : 123456789}>

  命令解釋:

  db.users.findOne() -->查詢某個文檔,可以添加具體的參數,只返回一個文檔,如上例子查詢文檔鍵name的值為cd的該文檔

  db.users.update() -->更新某個文檔,一般帶有兩個參數,一個參數定位到更新那個文檔,另一個參數設定修改內容

 {"$inc":{"key" : "value"}}-->修改鍵key對應的值,若key不存在,則建立key

 流程:

  1.首先查詢name為cd的文檔,看文檔裡面的具體索引值

  2.使用修改器$inc和更新語句更新集合中的文檔

  3.重新查詢name為cd的文檔,和步驟1的查詢結果對比,看更新文檔是否成功

2.$set修改器修改文檔

> db.users.findOne({"name":"scd"});{        "_id" : ObjectId("58549695a14618fbeef3bf0f"),        "name" : "scd",        "sex" : "M",        "address" : "上海"}> db.users.update( {"name":"scd"},...     {"$set":{"lang": ["中文", "english"]}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.findOne({"name":"scd"});{        "_id" : ObjectId("58549695a14618fbeef3bf0f"),        "name" : "scd",        "sex" : "M",        "address" : "上海",        "lang" : [                "中文",                "english"        ]}>

3.$uset修改器,刪除文檔中的某個鍵

> db.users.findOne({"name":"scd"});{        "_id" : ObjectId("58549885a14618fbeef3bf11"),        "name" : "scd",        "sex" : "M",        "address" : "上海",        "lang" : [                "中文",                "english"        ]}> db.users.update({"name":"scd"},...     {"$unset":{"sex":"M"}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.findOne({"name":"scd"});{        "_id" : ObjectId("58549885a14618fbeef3bf11"),        "name" : "scd",        "address" : "上海",        "lang" : [                "中文",                "english"        ]}>

操作結果:刪除鍵name值為scd文檔的鍵sex

 

4.$set和$inc修改器的區別

  $inc只能由於修改整型、長整型或雙精確度浮點型的值

 

> db.users.findOne({"name":"scd"});{        "_id" : ObjectId("58549885a14618fbeef3bf11"),        "name" : "scd",        "address" : "上海",        "lang" : [                "中文",                "english"        ]}> db.users.update({"name":"scd"},...    {"$inc":{"address": "北京"}});WriteResult({        "nMatched" : 0,        "nUpserted" : 0,        "nModified" : 0,        "writeError" : {                "code" : 14,                "errmsg" : "Cannot increment with non-numeric argument: {address: \"鍖椾含\"}"        }})

使用$inc修改鍵對應的數值為字串會報錯 "writeError"

而使用$set可以修改鍵對應的值為字元類型的數值

> db.users.findOne({"name":"scd"});{        "_id" : ObjectId("58549885a14618fbeef3bf11"),        "name" : "scd",        "address" : "上海",        "lang" : [                "中文",                "english"        ]}
> db.users.update({"name":"scd"},...    {"$set":{"address":"北京"}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.findOne("name":"scd");2016-12-17T09:55:20.213+0800 E QUERY    [thread1] SyntaxError: missing ) after argument list @(shell):1:23> db.users.findOne({"name":"scd"});{        "_id" : ObjectId("58549885a14618fbeef3bf11"),        "name" : "scd",        "address" : "北京",        "lang" : [                "中文",                "english"        ]}>

$set修改器可以修改許多類型的數值,如字串、數組等等

 

MongoDB--使用修改器修改文檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.