如何在插入时为子文档生成mongodb对象id

And*_*y N 6 mongodb nosql

插入也包含子文档的文档.我是通过cli这样做的.如何让mongodb为Alerts子文档记录生成对象ID?我想要类似下面的东西,但这不适用于cli.

db.user.insert(
{
    "Email" : "andy+5@domain.com",
    "Domain" : "other",
    "Type" : "local",
    "FName" : "Andy",
    "Alerts" : [ 
        {
            "_id" : new ObjectId(),
            "Keyword" : "sales",
            "Frequency" : {
                "Type" : "daily"
            },
            "IsActive" : true
        }, 
        {
            "_id" : new ObjectId(),
            "Keyword" : "customer service",
            "Frequency" : {
                "Type" : "daily"
            },
            "IsActive" : true
        } 
    ]
}
)
Run Code Online (Sandbox Code Playgroud)

小智 6

你可以在robomongo上的控制台上运行那个确切的脚本,它会执行得很好.不要在添加或编辑对话框中执行此操作.