PinkElephantsOnParade
PinkElephantsOnParade

Reputation: 6592

Complex Mongo Insert

Or..."complex to me" is maybe more accurate.

In any case, I have mongod and mongo open and in the mongo terminal I put in the following command:

db.err_details.insert({"error_text" : "Log has found error on inbound", "co
de" : "WM2001_0", "product" : "WM2001", "profile : ["CR" : "11999002", "sol
s" : ["run", "to", "the", "hills"]], "otherinfo" : "Contact Bob Saget"});

err_details DOES exist, not that that matters. In any case, I press enter and it gives me "..." twice then just quits without inserting. It has done this before when there is a syntax error in a query...yet it does not tell you where the error is or what is wrong, and, golly, I just can't find it this time.

In case the intention is unclear, I want "profile" to have an array of key-value pairs, one of which ("sols") has ITS OWN array just values, not key-value pairs.

In light of the fact "profile" is an object I tried the following:

db.err_details.insert({"error_text" : "Log has found error on inbound", "errco
de" : "WM2001_0", "product" : "WM2001", "profile" : {"CR" : "11999002", "sols" :
["run", "to", "the", "hills"]}, "otherinfo" : "Contact Bob Saget"});

This is valid mongo but I want an array of objects rather than an object containing an element with an array.

Upvotes: 0

Views: 1066

Answers (2)

JaredMcAteer
JaredMcAteer

Reputation: 22545

Arrays, [] in BSON/JSON/JavaScript cannot be keyed, you need to use an object {}. Your Profile property have keys but are typed as an array.

Upvotes: 1

Oleg V. Volkov
Oleg V. Volkov

Reputation: 22461

There's a syntax error. Your value for "profile" property seems to be an object, but you use array's []. And one of quote around profile is missing too.

(Hint: just copy/paste your command to any JS checking service, since that's what Mongo console uses. Editors with on-the-fly error highlighting will do exceptionally well).

I don't quite understand your DB's structure, but "array of k/v pairs" should be something along those lines:

db.err_details.insert({
    "error_text" : "Log has found error on inbound",
    "code" : "WM2001_0",
    "product" : "WM2001",
    "profile" : [
        {"CR" : "11999002"},
        {"sols" : ["run", "to", "the", "hills"]}
    ],
    "otherinfo" : "Contact Bob Saget"
});

i.e. - with every pair wrapped up in object.

Upvotes: 4

Related Questions