Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate mongodb object id for a sub-document on insert

Tags:

mongodb

nosql

Inserting a document that also has sub-documents. I am doing this through cli. How do I make mongodb generate a object id for the Alerts sub-document record? I want something like the following, but this does not work from cli.

db.user.insert(
{
    "Email" : "[email protected]",
    "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
        } 
    ]
}
)
like image 454
Andy N Avatar asked Mar 17 '14 23:03

Andy N


1 Answers

You can run that exact script on the console on robomongo, it would execute just fine. Don't do it on add or edit dialog.

like image 176
blubear Avatar answered Sep 20 '22 06:09

blubear