Discussion:
[mongodb-user] mongodb collections and embedded docs
armand pirvu
2018-12-05 03:02:06 UTC
Permalink
Hello



A noob with some questions. Any help is appreciated



Thks

-- Armand



case 1:

db.mymongo1_col1.insert( { item: "journal", qty: 25, size: { h: 14, w: 21,
uom: "cm" }, status: "A" });

db.mymongo1_col1.insert( { item: "notebook", qty: 25, size: { h: 14, w: 21,
uom: "cm" }, status: "A" });

db.mymongo1_col1.insert( { item: "notebook", qty: 25, size: { h: 14, w: 20,
uom: "cm" }, status: "A" });
db.mymongo1_col1.find();
{ "_id" : ObjectId("5c072f6834943268d8387edc"), "item" : "journal", "qty" :
25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }

{ "_id" : ObjectId("5c072f6834943268d8387edd"), "item" : "notebook", "qty"
: 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }

{ "_id" : ObjectId("5c072f6834943268d8387ede"), "item" : "notebook", "qty"
: 25, "size" : { "h" : 14, "w" : 20, "uom" : "cm" }, "status" : "A" }



case 2:


db.mymongo1_col2.insert({"_col1" : "3"}, {"_col2" : "col2_3", "_col3" :
"col3_3"});

db.mymongo1_col2.insert({"_col1" : "3"}, {"_col2" : "col2_3", "_col3" :
"col3_3"});

db.mymongo1_col2.insert({"_col1" : "3"}, {"_col2" : "col2_3", "_col3" :
"col3_4"});
db.mymongo1_col2.find();
{ "_id" : ObjectId("5c0734a434943268d8387edf"), "_col1" : "3" }

{ "_id" : ObjectId("5c0734a434943268d8387ee0"), "_col1" : "3" }

{ "_id" : ObjectId("5c0736fa34943268d8387ee1"), "_col1" : "3" }


case 3:


db.mymongo1_col3.insert({"_col1" : "3"}, "fld2": {"_col2" : "col2_3",
"_col3" : "col3_3"});

db.mymongo1_col3.insert({"_col1" : "3"}, "fld2": {"_col2" : "col2_3",
"_col3" : "col3_3"});

2018-12-04T21:22:51.497-0500 E QUERY [thread1] SyntaxError: missing )
after argument list @(shell):1:47



db.mymongo1_col3.insert({"_col1" : "3"}, "fld2": {"_col2" : "col2_3",
"_col3" : "col3_3"}});

db.mymongo1_col3.insert({"_col1" : "3"}, "fld2": {"_col2" : "col2_3",
"_col3" : "col3_3"}});



So some questions

Case 2 - why I don't see the embedded doc ? See the last on has "_col3" :
"col3_4"


Case 3 fails but Case 2 succeeded the only diff is prepending "fld2" from
what I can see. why ? However I think I see. see later case 3 in which i
start with two embeded docs vs case 5 am I right ?


db.mymongo1_col3.insert({"_col1" : "3"}, "fld2": {"_col2" : "col2_3",
"_col3" : "col3_3"});

db.mymongo1_col3.insert({"_col1" : "3"}, "fld2": {"_col2" : "col2_3",
"_col3" : "col3_4"});



Case 4

db.mymongo1_col4.insert( { _col1: "3" , size: { "_col2" : "col2_3", "_col3"
: "col3_3" }});

db.mymongo1_col4.insert( { _col1: "3", size: { "_col2" : "col2_3", "_col3"
: "col3_3" }});

db.mymongo1_col4.insert( { _col1: "3" , size: { "_col2" : "col2_3", "_col3"
: "col3_4"}});
db.mymongo1_col4.find();
{ "_id" : ObjectId("5c073bb834943268d8387ef9"), "_col1" : "3", "size" : {
"_col2" : "col2_3", "_col3" : "col3_3" } }

{ "_id" : ObjectId("5c073bb834943268d8387efa"), "_col1" : "3", "size" : {
"_col2" : "col2_3", "_col3" : "col3_3" } }

{ "_id" : ObjectId("5c073bb834943268d8387efb"), "_col1" : "3", "size" : {
"_col2" : "col2_3", "_col3" : "col3_4" } }




Case 5

db.mymongo1_col5.insert( { _col1: "3" , "fld2": { "_col2" : "col2_3",
"_col3" : "col3_3" }});

db.mymongo1_col5.insert( { _col1: "3", "fld2": { "_col2" : "col2_3",
"_col3" : "col3_3" }});

db.mymongo1_col5.insert( { _col1: "3", "fld2": { "_col2" : "col2_3",
"_col3" : "col3_4" }});
db.mymongo1_col5.find();
{ "_id" : ObjectId("5c073bf034943268d8387efc"), "_col1" : "3", "fld2" : {
"_col2" : "col2_3", "_col3" : "col3_3" } }

{ "_id" : ObjectId("5c073bf034943268d8387efd"), "_col1" : "3", "fld2" : {
"_col2" : "col2_3", "_col3" : "col3_3" } }

{ "_id" : ObjectId("5c073bf034943268d8387efe"), "_col1" : "3", "fld2" : {
"_col2" : "col2_3", "_col3" : "col3_4" } }
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/CAKrrPpiOTxxQmrOrfKULwi0o%2BcUg7aAKcPU08LOWZ6K%2BZMdA8g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
'Kevin Adistambha' via mongodb-user
2018-12-07 01:32:41 UTC
Permalink
Hi Armand

Case 2 - why I don’t see the embedded doc ? See the last on has “_col3” :
“col3_4”

This is because the statement you have: db.mymongo1_col2.insert({"_col1" :
"3"}, {"_col2" : "col2_3", "_col3" : "col3_3"}); is not what you want.
db.collection.insert()
<https://docs.mongodb.com/manual/reference/method/db.collection.insert/>
specifies the first parameter to be the document to be inserted, and the
second parameter to be the option document. In you example, your first
parameter is {col1: '3'}, thus MongoDB inserted that document. The rest of
the parameters were ignored since it was not recognized as valid options.

Case 3 fails but Case 2 succeeded the only diff is prepending “fld2” from
what I can see. why ?

This is the same reason as Case 2 above. db.collection.insert()
<https://docs.mongodb.com/manual/reference/method/db.collection.insert/>
requires the second parameter to be a valid JSON object. In your example,
you did not provide it with a valid JSON object, hence it returns an error.

In Case 4 and 5, you are providing a complete JSON document as the first
and only parameter to db.collection.insert(), so that document was inserted.

Best regards,
Kevin
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/9f483251-21a9-4abe-9175-bc1bcb2566a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
armand pirvu
2018-12-07 14:30:50 UTC
Permalink
Thank you Kevin

-- Armand

On Thu, Dec 6, 2018 at 8:32 PM 'Kevin Adistambha' via mongodb-user <
Post by 'Kevin Adistambha' via mongodb-user
Hi Armand
“col3_4”
This is because the statement you have: db.mymongo1_col2.insert({"_col1"
: "3"}, {"_col2" : "col2_3", "_col3" : "col3_3"}); is not what you want.
db.collection.insert()
<https://docs.mongodb.com/manual/reference/method/db.collection.insert/>
specifies the first parameter to be the document to be inserted, and the
second parameter to be the option document. In you example, your first
parameter is {col1: '3'}, thus MongoDB inserted that document. The rest
of the parameters were ignored since it was not recognized as valid options.
Case 3 fails but Case 2 succeeded the only diff is prepending “fld2” from
what I can see. why ?
This is the same reason as Case 2 above. db.collection.insert()
<https://docs.mongodb.com/manual/reference/method/db.collection.insert/>
requires the second parameter to be a valid JSON object. In your example,
you did not provide it with a valid JSON object, hence it returns an error.
In Case 4 and 5, you are providing a complete JSON document as the first
and only parameter to db.collection.insert(), so that document was inserted.
Best regards,
Kevin
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups
"mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit
https://groups.google.com/d/msgid/mongodb-user/9f483251-21a9-4abe-9175-bc1bcb2566a3%40googlegroups.com
<https://groups.google.com/d/msgid/mongodb-user/9f483251-21a9-4abe-9175-bc1bcb2566a3%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/CAKrrPpjkEknLGJwoZxoSEyvtZxvYsGWDmOnPBNiS-QvxR7ZeVw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...