【问题标题】:How can I use a new meteor id when inserting using the meteor mongo shell?使用流星 mongo shell 插入时如何使用新的流星 ID?
【发布时间】:2016-09-20 14:46:42
【问题描述】:

如果我运行 meteor mongo 然后在 shell 中输入

meteor:PRIMARY> db.collection_name.insert({field:"string"})

我得到一个带有 ObjectId 的文档,但我希望它有一个 Meteor id 字符串。

{ "_id" : ObjectId("57e1492daf102b5f83d5e683"), "field":"string" }

我试过这个meteor:PRIMARY> db.dictionary.insert({_id:new Mongo.ObjectId, "field":"string"}) 但当然不行。

如何在 mongo shell 中插入一个文档并给它一个唯一的随机 id 字符串,就像它在代码中插入时一样?

像这样:{ "_id" : "xGfyoJE6GBPqG6Av8", "field":"string" }

【问题讨论】:

  • 是的,我看到了。如果这意味着没有办法做到这一点,那么什么是好的解决方法?我正在考虑使用可能使用随机生成器的文本替换应用程序。如果这可行,那么获得与数据库中相同的随机字符串的机会有多大?我想这并不重要,因为您可以重新运行命令。

标签: mongodb meteor


【解决方案1】:

您可以使用 Mongo 内置方法 ObjectId() https://docs.mongodb.com/manual/reference/method/ObjectId/

db.messages.insert({_id: ObjectId(), "fieldtype": "string"})

这会给你你想要的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 2014-03-11
    • 2017-06-02
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多