比如有下面一个集合articles:
{"_id" : ObjectId("98r498jfgragaja9ra4"),"titile":"foo","content":"foo..."}
{"_id" : ObjectId("498aut89auhgua4940q"),"titile":"bar","content":"bar..."}
{"_id" : ObjectId("30ua9ah984yhtauiga9"),"titile":"hello","content":"hello..."}
我想给它增加一个从1开始的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"}
应该怎么做呢?
你可能要先考虑一下为什么需要这个编号。如果只是需要递增字段,_id使用的ObjectId就是递增的。而使用数字序列为什么有问题,我在这个问题里面做过解释。