我想在集合中添加id
字段,该数字从1
开始。
例如,
有一个收藏文章:
{"_id" : ObjectId("98r498jfgragaja9ra4"),"titile":"foo","content":"foo..."}
{"_id" : ObjectId("498aut89auhgua4940q"),"titile":"bar","content":"bar..."}
{"_id" : ObjectId("30ua9ah984yhtauiga9"),"titile":"hello","content":"hello..."}
添加id
字段后,结果如下所示:
{"_id" : ObjectId("98r498jfgragaja9ra4"),"titile":"foo","content":"foo...","id":"1"}
{"_id" : ObjectId("498aut89auhgua4940q"),"titile":"bar","content":"bar...","id":"2"}
{"_id" : ObjectId("30ua9ah984yhtauiga9"),"titile":"hello","content":"hello...","id":"3"}
怎么做?
答案 0 :(得分:0)
MongoDB的文档提供了一些有关如何执行此操作的建议:
https://docs.mongodb.com/v3.0/tutorial/create-an-auto-incrementing-field/