Sub documents
>
db.num.insert({x:10,"Name":{"FName":"tim","LName":"Southee"}})
WriteResult({
"nInserted" : 1 })
>
db.num.find({'Name.FName':"tim"}).pretty()
{
"_id" :
ObjectId("55911994525a7a85c0902ff7"),
"x" : 10,
"Name" : {
"FName" :
"tim",
"LName" :
"Southee"
}
}
>
>
db.num.insert({x:10,"Name":{"FName":"tim","LName":"Southee"},"Qualif":["Bca","
Mca"]})
WriteResult({
"nInserted" : 1 })
>
db.num.find({'Qualif':"Bca"}).pretty()
{
"_id" :
ObjectId("55911cf9525a7a85c0902ff8"),
"x" : 10,
"Name" : {
"FName" :
"tim",
"LName" :
"Southee"
},
"Qualif" : [
"Bca",
"Mca"
]
}
>
Regex
>
db.Employee.find({"EmpName":{$regex:/^S/i}}).pretty()
{
"_id" :
ObjectId("55910c4f525a7a85c0902ff5"),
"EmpName" :
"Supreet",
"Designation" : "Senior
Software Eng"
}
Array
>
db.myproj.insert({"Project":{"project1":["wbs1","wbs2"]}})
WriteResult({
"nInserted" : 1 })
>
db.myproj.find({'Project.project1':"wbs1"}).pretty()
{
"_id" :
ObjectId("55911f67525a7a85c0902ffa"),
"Project" : {
"project1" : [
"wbs1",
"wbs2"
]
}
}
>
db.myproj.insert({"Project":{"project2":["wbs1","wbs2"]}})
WriteResult({
"nInserted" : 1 })
>
>
db.myproj.find().pretty()
{
"_id" :
ObjectId("55911f17525a7a85c0902ff9"),
"Project" : [
"wbs1",
"wbs2"
]
}
{
"_id" :
ObjectId("55911f67525a7a85c0902ffa"),
"Project" : {
"project1" : [
"wbs1",
"wbs2"
]
}
}
{
"_id" :
ObjectId("55911fee525a7a85c0902ffb"),
"Project" : {
"project2" : [
"wbs1",
"wbs2"
]
}
}
>
Orders collection :
selecting from an array
>
db.orders.insert({"memos":[{"memo":"on
time","by":"payment"},{"memo":"delayed","by":"shipping"}]})
WriteResult({
"nInserted" : 1 })
>
db.orders.find().pretty()
{
"_id" :
ObjectId("55912599525a7a85c0902ffc"),
"memos" : [
{
"memo" :
"on time",
"by" :
"shipping"
},
{
"memo" :
"approved",
"by" :
"billing"
}
]
}
{
"_id" :
ObjectId("559125cb525a7a85c0902ffd"),
"memos" : [
{
"memo" :
"on time",
"by" :
"payment"
},
{
"memo" :
"delayed",
"by" :
"shipping"
}
]
}
>
db.orders.find({'memos.0.by':'shipping'}).pretty()
{
"_id" :
ObjectId("55912599525a7a85c0902ffc"),
"memos" : [
{
"memo" :
"on time",
"by" :
"shipping"
},
{
"memo" :
"approved",
"by" :
"billing"
}
]
}
>
db.orders.find({'memos.by':'shipping'}).pretty()
{
"_id" :
ObjectId("55912599525a7a85c0902ffc"),
"memos" : [
{
"memo" :
"on time",
"by" :
"shipping"
},
{
"memo" :
"approved",
"by" :
"billing"
}
]
}
{
"_id" :
ObjectId("559125cb525a7a85c0902ffd"),
"memos" : [
{
"memo" :
"on time",
"by" :
"payment"
},
{
"memo" :
"delayed",
"by" :
"shipping"
}
]
}
No comments:
Post a Comment