有沒有辦法在 MongoDB 的一個語句中添加遞增的 id? (Is there a way to add an incrementing id in one statement in MongoDB?)


問題描述

有沒有辦法在 MongoDB 的一個語句中添加遞增的 id? (Is there a way to add an incrementing id in one statement in MongoDB?)

所以我有一個小型數據庫,它不會增長太多,我正試圖從我用 python 實現的 API 中的數據庫中獲取一個文檔,以便使用給定的文檔 ID 我在D b。但是,我發現讓用戶從數據庫中寫入一個隨機數有點困難。我所需要的只是一個通過設置 id 字段和自動增量來修改每個文檔的函數。正如我所說,它不會增長那麼多,性能也不是問題。

到目前為止,我能做的是:

var i = 0
db.MyCollection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}
i ++;),

我實現了設置一個 id 字段,但它為每個文檔設置了相同的數字(每個文檔的計數)所以假設數據庫有 10 個文檔,它會將 ID 設置為 10。


參考解法

方法 1:

Find‑and‑modify operation returns the document updated (before or after the update depending on returnDocument setting). You can use this with $inc to implement a counter. Ruby example where c is a collection:

irb(main):005:0> c['foo'].insert_one(counter:true,count:1)
=> #<Mongo::Operation::Insert::Result:0x8040 documents=[{"n"=>1, "opTime"=>{"ts"=>#<BSON::Timestamp:0x00005609f260b7e0 @seconds=1594961771, @increment=2>, "t"=>1}, "electionId"=>BSON::ObjectId('7fffffff0000000000000001'), "ok"=>1.0, "$clusterTime"=>{"clusterTime"=>#<BSON::Timestamp:0x00005609f260b538 @seconds=1594961771, @increment=2>, "signature"=>{"hash"=><BSON::Binary:0x8060 type=generic data=0x0000000000000000...>, "keyId"=>0}}, "operationTime"=>#<BSON::Timestamp:0x00005609f260b290 @seconds=1594961771, @increment=2>}]>

irb(main):011:0> c['foo'].find_one_and_update({counter:true},{'$inc':{count:1}})
=> {"_id"=>BSON::ObjectId('5f112f6b2c97a6281f63f575'), "counter"=>true, "count"=>1}
irb(main):012:0> c['foo'].find_one_and_update({counter:true},{'$inc':{count:1}})
=> {"_id"=>BSON::ObjectId('5f112f6b2c97a6281f63f575'), "counter"=>true, "count"=>2}
irb(main):013:0> c['foo'].find_one_and_update({counter:true},{'$inc':{count:1}})
=> {"_id"=>BSON::ObjectId('5f112f6b2c97a6281f63f575'), "counter"=>true, "count"=>3}
irb(main):014:0> c['foo'].find_one_and_update({counter:true},{'$inc':{count:1}})
=> {"_id"=>BSON::ObjectId('5f112f6b2c97a6281f63f575'), "counter"=>true, "count"=>4}

方法 2:

Why not just use this logic? Instead of updating all via one query, just launch multiple queries one by one? Mongo will do it pretty fast, even if you have >1M docs in database (according to your phrase: I got a small database) because pre‑builded index on _id field.

this is a javasript code, but I guess, you'll understand the logic of it

let all_documents = db.MyCollection.find({});

for (let i = 0; i < all_documents.length; i++) {
  db.MyCollection.update({_id: all_documents[i]._id }, {$set : {"new_field": i}}, {upsert:false})
}

(by Diego DuarteD. SMAlexZeDim)

參考文件

  1. Is there a way to add an incrementing id in one statement in MongoDB? (CC BY‑SA 2.5/3.0/4.0)

#mongo-shell #pymongo #mongoDB #python-3.x






相關問題

Windows 是否有任何 mongo shell 擴展? (Is there any mongo shell extensions for windows?)

獲取查詢中所有文檔的大小 (Get the size of all the documents in a query)

如何從 java 程序運行 mongo 查詢? (How to run a mongo query from java program?)

MongoDB 更新 ObjectId 字段 (MongoDB update ObjectId field)

MongoDB - 將簡單字段更改為對象 (MongoDB - change simple field into an object)

mongoDB aggregate() 在電子郵件對象集合中查找電子郵件時間 (mongoDB aggregate() finding email times in a collection of email objects)

指定在 mongodb .js 腳本中使用哪個數據庫 (Specify which database to use in mongodb .js script)

命令失敗:MongoError:CMD_NOT_ALLOWED:配置文件 (command failed: MongoError: CMD_NOT_ALLOWED: profile)

有沒有辦法在 MongoDB 的一個語句中添加遞增的 id? (Is there a way to add an incrementing id in one statement in MongoDB?)

運行 rs.initiate() 後 mongodb 副本集錯誤“...replSetHeartbeat 需要身份驗證...” (mongodb replica set error "...replSetHeartbeat requires authentication..." after running rs.initiate())

將 mongo shell 連接到受限的 MongoDB Atlas 數據庫? (Connect mongo shell to restricted MongoDB Atlas Database?)

[MongoDB]:更改所有數組字段中的值類型 ([MongoDB]: Changing the type of values in all array fields)







留言討論