Related
Using the following command I get all the duplicate values in my collection based on the "no" field:
db.books.aggregate([{ $group: { _id: {no: "$no"}, uniqueIds: {$addToSet: "$_id"}, count: {$sum: 1}}}, { $match: { count: {"$gt": 1}}}, { $sort: {count: -1}}])
This returns the following:
{ "_id" : { "no" : 160541455 }, "uniqueIds" : [ ObjectId("5b3a3a3bd6194c5bfe40a523"), ObjectId("5b3a345dd6194c5bfe3deefa"), ObjectId("5b3a3509d6194c5bfe3e2e29"), ObjectId("5b3a3440d6194c5bfe3de3b4"), ObjectId("5b3a336ad6194c5bfe3d80a9"), ObjectId("5b3a3600d6194c5bfe3eb073"), ObjectId("5b3a37e1d6194c5bfe3f96f0"), ObjectId("5b3a35aed6194c5bfe3e8b83"), ObjectId("5b3a36ead6194c5bfe3f2dbd"), ObjectId("5b3a3ea6d6194c5bfe42d794"), ObjectId("5b3a3731d6194c5bfe3f50d2"), ObjectId("5b3a399bd6194c5bfe40676c") ], "count" : 12 }
{ "_id" : { "no" : 105900593 }, "uniqueIds" : [ ObjectId("5b3a3efad6194c5bfe42fc1f"), ObjectId("5b3a3df3d6194c5bfe429191"), ObjectId("5b3a3697d6194c5bfe3f0ca9"), ObjectId("5b3a385fd6194c5bfe3fdced"), ObjectId("5b3a3b69d6194c5bfe416901"), ObjectId("5b3a347ed6194c5bfe3dfeff"), ObjectId("5b3a335ad6194c5bfe3d7c15"), ObjectId("5b3a3887d6194c5bfe3feaad"), ObjectId("5b3a38b8d6194c5bfe400088"), ObjectId("5b3a358dd6194c5bfe3e81ea"), ObjectId("5b3a3b3fd6194c5bfe4157bc"), ObjectId("5b3a3decd6194c5bfe428efa") ], "count" : 12 }
{ "_id" : { "no" : 144919593 }, "uniqueIds" : [ ObjectId("5b3a3e46d6194c5bfe42b277"), ObjectId("5b3a3c22d6194c5bfe41b05a"), ObjectId("5b3a398cd6194c5bfe4065be"), ObjectId("5b3a37a1d6194c5bfe3f8239"), ObjectId("5b3a3678d6194c5bfe3f0496"), ObjectId("5b3a33d2d6194c5bfe3db42c"), ObjectId("5b3a36e6d6194c5bfe3f2d4d"), ObjectId("5b3a3d05d6194c5bfe421e97"), ObjectId("5b3a384ad6194c5bfe3fd621"), ObjectId("5b3a3565d6194c5bfe3e6c44") ], "count" : 10 }
{ "_id" : { "no" : 86204442 }, "uniqueIds" : [ ObjectId("5b3a3df5d6194c5bfe429204"), ObjectId("5b3a3d1cd6194c5bfe4224a1"), ObjectId("5b3a3b33d6194c5bfe415288"), ObjectId("5b3a3934d6194c5bfe403ed9"), ObjectId("5b3a3c39d6194c5bfe41b664"), ObjectId("5b3a37dbd6194c5bfe3f961c"), ObjectId("5b3a3688d6194c5bfe3f0613"), ObjectId("5b3a3731d6194c5bfe3f50c6"), ObjectId("5b3a37dfd6194c5bfe3f9697") ], "count" : 9 }
{ "_id" : { "no" : 119417424 }, "uniqueIds" : [ ObjectId("5b3a3b34d6194c5bfe4152a5"), ObjectId("5b3a392cd6194c5bfe403b21"), ObjectId("5b3a399ad6194c5bfe406736"), ObjectId("5b3a38b9d6194c5bfe4000c7"), ObjectId("5b3a3ea5d6194c5bfe42d77f"), ObjectId("5b3a374cd6194c5bfe3f5c9f"), ObjectId("5b3a33d9d6194c5bfe3db4e3"), ObjectId("5b3a3386d6194c5bfe3d8ab8"), ObjectId("5b3a3857d6194c5bfe3fd754") ], "count" : 9 }
{ "_id" : { "no" : 159374794 }, "uniqueIds" : [ ObjectId("5b3a3e52d6194c5bfe42b3b8"), ObjectId("5b3a3c0fd6194c5bfe41ac10"), ObjectId("5b3a38f5d6194c5bfe401b2f"), ObjectId("5b3a37afd6194c5bfe3f83b7"), ObjectId("5b3a33ebd6194c5bfe3dbc27"), ObjectId("5b3a36edd6194c5bfe3f2e78"), ObjectId("5b3a34ced6194c5bfe3e1b24"), ObjectId("5b3a3b36d6194c5bfe41530d"), ObjectId("5b3a3753d6194c5bfe3f5f99") ], "count" : 9 }
{ "_id" : { "no" : 159363089 }, "uniqueIds" : [ ObjectId("5b3a3e4fd6194c5bfe42b340"), ObjectId("5b3a3e0ad6194c5bfe429a1f"), ObjectId("5b3a3854d6194c5bfe3fd708"), ObjectId("5b3a34cbd6194c5bfe3e1acb"), ObjectId("5b3a38f4d6194c5bfe401afb"), ObjectId("5b3a36c4d6194c5bfe3f20aa"), ObjectId("5b3a3a06d6194c5bfe409119"), ObjectId("5b3a36ecd6194c5bfe3f2e2b"), ObjectId("5b3a3764d6194c5bfe3f6439") ], "count" : 9 }
{ "_id" : { "no" : 101412948 }, "uniqueIds" : [ ObjectId("5b3a3ea0d6194c5bfe42d708"), ObjectId("5b3a37b7d6194c5bfe3f8749"), ObjectId("5b3a3712d6194c5bfe3f4510"), ObjectId("5b3a334ed6194c5bfe3d759b"), ObjectId("5b3a3929d6194c5bfe403aca"), ObjectId("5b3a36f3d6194c5bfe3f334d"), ObjectId("5b3a39bad6194c5bfe407541"), ObjectId("5b3a3477d6194c5bfe3df66b"), ObjectId("5b3a35bdd6194c5bfe3e9233") ], "count" : 9 }
{ "_id" : { "no" : 107412155 }, "uniqueIds" : [ ObjectId("5b3a3e52d6194c5bfe42b3d1"), ObjectId("5b3a3a3bd6194c5bfe40a53a"), ObjectId("5b3a399cd6194c5bfe40678d"), ObjectId("5b3a390fd6194c5bfe402698"), ObjectId("5b3a3c0fd6194c5bfe41ac28"), ObjectId("5b3a346ad6194c5bfe3df338"), ObjectId("5b3a3584d6194c5bfe3e718a"), ObjectId("5b3a373cd6194c5bfe3f5563"), ObjectId("5b3a37e5d6194c5bfe3f97d6") ], "count" : 9 }
{ "_id" : { "no" : 156426829 }, "uniqueIds" : [ ObjectId("5b3a3cc6d6194c5bfe421829"), ObjectId("5b3a3f01d6194c5bfe430069"), ObjectId("5b3a3a29d6194c5bfe40a378"), ObjectId("5b3a37c9d6194c5bfe3f8fdc"), ObjectId("5b3a3558d6194c5bfe3e6b19"), ObjectId("5b3a3dabd6194c5bfe428666"), ObjectId("5b3a366ad6194c5bfe3f0356"), ObjectId("5b3a36e3d6194c5bfe3f2d02"), ObjectId("5b3a3798d6194c5bfe3f8160") ], "count" : 9 }
{ "_id" : { "no" : 104966976 }, "uniqueIds" : [ ObjectId("5b3a3c35d6194c5bfe41b614"), ObjectId("5b3a3a04d6194c5bfe4090e9"), ObjectId("5b3a3d19d6194c5bfe422451"), ObjectId("5b3a37a7d6194c5bfe3f82bc"), ObjectId("5b3a348cd6194c5bfe3e034c"), ObjectId("5b3a3e58d6194c5bfe42b5d2"), ObjectId("5b3a345cd6194c5bfe3deee2"), ObjectId("5b3a35aad6194c5bfe3e8b15"), ObjectId("5b3a36c3d6194c5bfe3f208b") ], "count" : 9 }
{ "_id" : { "no" : 105460396 }, "uniqueIds" : [ ObjectId("5b3a3e04d6194c5bfe429777"), ObjectId("5b3a3b99d6194c5bfe417eb6"), ObjectId("5b3a38c7d6194c5bfe400533"), ObjectId("5b3a3e6ed6194c5bfe42bbc6"), ObjectId("5b3a38f5d6194c5bfe401b1d"), ObjectId("5b3a3ee8d6194c5bfe42f43f"), ObjectId("5b3a39a5d6194c5bfe4069ec"), ObjectId("5b3a3a07d6194c5bfe409134") ], "count" : 8 }
{ "_id" : { "no" : 158805980 }, "uniqueIds" : [ ObjectId("5b3a3efad6194c5bfe42fc7c"), ObjectId("5b3a3abed6194c5bfe41470a"), ObjectId("5b3a337cd6194c5bfe3d89c3"), ObjectId("5b3a33bdd6194c5bfe3db218"), ObjectId("5b3a3dfcd6194c5bfe42955f"), ObjectId("5b3a3be7d6194c5bfe41a820"), ObjectId("5b3a3444d6194c5bfe3de564"), ObjectId("5b3a391fd6194c5bfe4039d5") ], "count" : 8 }
{ "_id" : { "no" : 107411546 }, "uniqueIds" : [ ObjectId("5b3a3c0fd6194c5bfe41ac2a"), ObjectId("5b3a3b37d6194c5bfe415337"), ObjectId("5b3a390fd6194c5bfe402699"), ObjectId("5b3a399cd6194c5bfe406793"), ObjectId("5b3a373cd6194c5bfe3f5565"), ObjectId("5b3a346ad6194c5bfe3df339"), ObjectId("5b3a3584d6194c5bfe3e718b"), ObjectId("5b3a37e5d6194c5bfe3f97d7") ], "count" : 8 }
{ "_id" : { "no" : 128015326 }, "uniqueIds" : [ ObjectId("5b3a3ed1d6194c5bfe42f1a3"), ObjectId("5b3a3df4d6194c5bfe4291f9"), ObjectId("5b3a38bed6194c5bfe400437"), ObjectId("5b3a381dd6194c5bfe3fd124"), ObjectId("5b3a3465d6194c5bfe3df1a7"), ObjectId("5b3a36cdd6194c5bfe3f2569"), ObjectId("5b3a3719d6194c5bfe3f4637"), ObjectId("5b3a377bd6194c5bfe3f7dfa") ], "count" : 8 }
{ "_id" : { "no" : 123932233 }, "uniqueIds" : [ ObjectId("5b3a3df3d6194c5bfe429177"), ObjectId("5b3a3d1dd6194c5bfe4224be"), ObjectId("5b3a3c39d6194c5bfe41b681"), ObjectId("5b3a3c0fd6194c5bfe41ac0d"), ObjectId("5b3a34e2d6194c5bfe3e22e6"), ObjectId("5b3a349fd6194c5bfe3e077d"), ObjectId("5b3a35aed6194c5bfe3e8b86"), ObjectId("5b3a3886d6194c5bfe3fea0f") ], "count" : 8 }
{ "_id" : { "no" : 150567857 }, "uniqueIds" : [ ObjectId("5b3a3a97d6194c5bfe413812"), ObjectId("5b3a39d9d6194c5bfe408888"), ObjectId("5b3a3816d6194c5bfe3fcd1b"), ObjectId("5b3a3635d6194c5bfe3ef80f"), ObjectId("5b3a3480d6194c5bfe3e011d"), ObjectId("5b3a34ead6194c5bfe3e2661"), ObjectId("5b3a33f8d6194c5bfe3dc477"), ObjectId("5b3a3529d6194c5bfe3e616a") ], "count" : 8 }
{ "_id" : { "no" : 143815726 }, "uniqueIds" : [ ObjectId("5b3a3b46d6194c5bfe415985"), ObjectId("5b3a3a08d6194c5bfe409172"), ObjectId("5b3a39a5d6194c5bfe4069fd"), ObjectId("5b3a33dad6194c5bfe3db527"), ObjectId("5b3a399cd6194c5bfe4067e7"), ObjectId("5b3a348fd6194c5bfe3e03a3"), ObjectId("5b3a37bcd6194c5bfe3f89c6"), ObjectId("5b3a36b7d6194c5bfe3f1cb9") ], "count" : 8 }
{ "_id" : { "no" : 143839331 }, "uniqueIds" : [ ObjectId("5b3a3ee8d6194c5bfe42f43a"), ObjectId("5b3a3e6ed6194c5bfe42bbbd"), ObjectId("5b3a3e04d6194c5bfe429773"), ObjectId("5b3a38c7d6194c5bfe40052f"), ObjectId("5b3a3a07d6194c5bfe40912e"), ObjectId("5b3a38f5d6194c5bfe401b19"), ObjectId("5b3a3b99d6194c5bfe417eb0"), ObjectId("5b3a39a5d6194c5bfe4069e8") ], "count" : 8 }
{ "_id" : { "no" : 99764925 }, "uniqueIds" : [ ObjectId("5b3a39f7d6194c5bfe408db1"), ObjectId("5b3a39c0d6194c5bfe4075eb"), ObjectId("5b3a399cd6194c5bfe40678a"), ObjectId("5b3a37bcd6194c5bfe3f89bf"), ObjectId("5b3a368ad6194c5bfe3f0673"), ObjectId("5b3a348fd6194c5bfe3e0391"), ObjectId("5b3a3578d6194c5bfe3e6e39"), ObjectId("5b3a36b7d6194c5bfe3f1ca8") ], "count" : 8 }
Question: What is the easiest and quickest way to remove the duplicates in Mongo 3.6.5? How must I edit the above command to achieve this?
I have tried the following but it doesn't work:
db.books.aggregate([ { $group:{ _id: {no: "$no" }, uniqueIds: { $addToSet: "$_id" }, count: {$sum: 1} } }, { $match: {count: {$gt: 1} } } ]).forEach(function(doc){ doc.uniqueIds.shift(); db.uniqueIds.remove({_id : {$in: doc.uniqueIds}});})
Mongo-3.6: Here ReviewId is my Unique Key, want to Delete my duplicate reviewId value
cursor=db.reviewsAnalysed.aggregate([{"$group": {"_id": {"reviewId":"$reviewId"},
"unique_ids": {"$addToSet": "$_id"}, "count": {"$sum": 1 } } }])
response = []
for doc in cursor:
del doc["unique_ids"][0]
for id in doc["unique_ids"]:
response.append(id)
db.reviewsAnalysed.remove({"_id": {"$in": response}})
I am new to mongodb, I have a dataset that looks like the following, and I'm trying to Write an aggregation query that will determine the number of unique companies with which an individual has been associated.
Schema:
{
"_id" : ObjectId("52cdef7c4bab8bd675297d8b"),
"name" : "AdventNet",
"permalink" : "abc3",
"crunchbase_url" : "http://www.crunchbase.com/company/adventnet",
"homepage_url" : "http://adventnet.com",
"blog_url" : "",
"blog_feed_url" : "",
"twitter_username" : "manageengine",
"category_code" : "enterprise",
"number_of_employees" : 600,
"founded_year" : 1996,
"deadpooled_year" : 2,
"tag_list" : "",
"alias_list" : "Zoho ManageEngine ",
"email_address" : "pr#adventnet.com",
"phone_number" : "925-924-9500",
"description" : "Server Management Software",
"created_at" : ISODate("2007-05-25T19:24:22Z"),
"updated_at" : "Wed Oct 31 18:26:09 UTC 2012",
"overview" : "<p>AdventNet is now Zoho ManageEngine.</p>\n\n<p>Founded in 1996, AdventNet has served a diverse range of enterprise IT, networking and telecom customers.</p>\n\n<p>AdventNet supplies server and network management software.</p>",
"image" : {
"available_sizes" : [
[
[
150,
55
],
"assets/images/resized/0001/9732/19732v1-max-150x150.png"
],
[
[
150,
55
],
"assets/images/resized/0001/9732/19732v1-max-250x250.png"
],
[
[
150,
55
],
"assets/images/resized/0001/9732/19732v1-max-450x450.png"
]
]
},
"products" : [ ],
"relationships" : [
{
"is_past" : true,
"title" : "CEO and Co-Founder",
"person" : {
"first_name" : "Sridhar",
"last_name" : "Vembu",
"permalink" : "sridhar-vembu"
}
},
{
"is_past" : true,
"title" : "VP of Business Dev",
"person" : {
"first_name" : "Neil",
"last_name" : "Butani",
"permalink" : "neil-butani"
}
},
{
"is_past" : true,
"title" : "Usabiliy Engineer",
"person" : {
"first_name" : "Bharath",
"last_name" : "Balasubramanian",
"permalink" : "bharath-balasibramanian"
}
},
{
"is_past" : true,
"title" : "Director of Engineering",
"person" : {
"first_name" : "Rajendran",
"last_name" : "Dandapani",
"permalink" : "rajendran-dandapani"
}
},
{
"is_past" : true,
"title" : "Market Analyst",
"person" : {
"first_name" : "Aravind",
"last_name" : "Natarajan",
"permalink" : "aravind-natarajan"
}
},
{
"is_past" : true,
"title" : "Director of Product Management",
"person" : {
"first_name" : "Hyther",
"last_name" : "Nizam",
"permalink" : "hyther-nizam"
}
},
{
"is_past" : true,
"title" : "Western Regional OEM Sales Manager",
"person" : {
"first_name" : "Ian",
"last_name" : "Wenig",
"permalink" : "ian-wenig"
}
}
],
"competitions" : [ ],
"providerships" : [
{
"title" : "DHFH",
"is_past" : true,
"provider" : {
"name" : "A Small Orange",
"permalink" : "a-small-orange"
}
}
],
"total_money_raised" : "$0",
"funding_rounds" : [ ],
"investments" : [ ],
"acquisition" : null,
"acquisitions" : [ ],
"offices" : [
{
"description" : "Headquarters",
"address1" : "4900 Hopyard Rd.",
"address2" : "Suite 310",
"zip_code" : "94588",
"city" : "Pleasanton",
"state_code" : "CA",
"country_code" : "USA",
"latitude" : 37.692934,
"longitude" : -121.904945
}
],
"milestones" : [ ],
"video_embeds" : [ ],
"screenshots" : [
{
"available_sizes" : [
[
[
150,
94
],
"assets/images/resized/0004/3400/43400v1-max-150x150.png"
],
[
[
250,
156
],
"assets/images/resized/0004/3400/43400v1-max-250x250.png"
],
[
[
450,
282
],
"assets/images/resized/0004/3400/43400v1-max-450x450.png"
]
],
"attribution" : null
}
],
"external_links" : [ ],
"partners" : [ ]
}
Here is the query I tried:
db.companies.aggregate([{
$match: {
"relationships.person": {
$ne: null
}
}
}, {
$project: {
relationships: 1,
_id: 0
}
}, {
$unwind: "$relationships"
}, {
$group: {
_id: "$relationships.person",
count: {
$addToSet: "$relationships"
}
}
}])
I think I now need to get the length of the $relationships array? How would I do that?
When you only want the size of the array you really don't need to unwind...
Just use $size.
Alter your aggregation to:
db.companies.aggregate([{
$match: {
"relationships.person": {
$ne: null
}
}
}, {
$project: {
relationships: 1,
_id: 0,
relationship_size : { $size : "$relationships"}
}
}
}])
This should give you the result you want
From the comment i understand you want some more logic in the aggregation, from outta my head i would alter your aggregation to:
db.companies.aggregate([{
$match: {
"relationships.person": {
$ne: null
}
}
}, {
$project: {
relationships: 1,
_id: 0
}
}, {
$unwind: "$relationships"
}, {
$group: {
_id: "$relationships.person.permalink",
count : {$sum : 1}
}
}])
I can't find a "company name" in your relationships array so i use the permalink property
I have a MongoDB collection that has document in the following format:
result of MyCollection.find({"_id": "20141029"})
{
"_id" : "20141029",
"portfolio_returns" : [
{
"data" : [
{
"report" : {
"open" : {
"returns_rs" : 398.8
},
"both" : {
"returns_rs" : 1054.800
}
},
"portfolio" : "Mystocks"
}
],
"user" : "mzmmohideen#gmail.com"
},
{
"data" : [
{
"report" : {
"open" : {
"returns_rs" : 5000
},
"both" : {
"returns_rs" : 5500
}
},
"portfolio" : "pff"
}
],
"user" : "mani#ithoughtz.com"
}
],
'portfolio_stock_gain_or_loss': [
{
"data" : [
{
"portfolio" : "gm",
"stocks" : [ ]
}
],
"user" : "moorthy.gm#gmail.com"
},
{
"data" : [
{
"portfolio" : "Mystocks",
"stocks" : [
{
"open" : {
"returns_rs" : 9144
},
"scripcode" : "532540",
"both" : {
"returns_rs" : 9426.75
}
},
{
"open" : {
"returns_rs" : 254.80
},
"scripcode" : "500790",
"both" : {
"returns_rs" : 254.80
}
}
]
}
],
"user" : "mzmmohideen#gmail.com"
},
{
"data" : [
{
"portfolio" : "pff",
"stocks" : [
{
"open" : {
"returns_rs" : 4000
},
"scripcode" : "500790",
"both" : {
"returns_rs" : 8500
}
},
{
"open" : {
"returns_rs" : 0
},
"scripcode" : "533151",
"both" : {
"returns_rs" : 0
}
}
]
}
],
"user" : "mani#ithoughtz.com"
}
]
}
My question is how can I filter out data from this doccument that have "portfolio_stock_gain_or_loss.data.stocks.open.returns_rs" greater than 1000 using pymongo.
expecting out put is
{
"_id" : "20141029",
'portfolio_stock_gain_or_loss': [
{
"data" : [
{
"portfolio" : "Mystocks",
"stocks" : [
{
"open" : {
"returns_rs" : 9144
},
"scripcode" : "532540",
"both" : {
"returns_rs" : 9426.75
}
}
]
}
],
"user" : "mzmmohideen#gmail.com"
},
{
"data" : [
{
"portfolio" : "pff",
"stocks" : [
{
"open" : {
"returns_rs" : 4000
},
"scripcode" : "500790",
"both" : {
"returns_rs" : 8500
}
},
]
}
],
"user" : "mani#ithoughtz.com"
}
]
}
Any ideas how I can do this on existing data or should I need to change the format?
Many thanks
Hi your mongo documents are too nested I think if possible you should restructure your mongo documents. Otherwise if you want to continue with same documents then mongo aggregation help full for solving your problem. I write following mongo aggregation I think it solves your problem
db.MyCollection.aggregate([
{"$unwind":"$portfolio_stock_gain_or_loss"},
{"$unwind":"$portfolio_stock_gain_or_loss.data"},
{"$unwind":"$portfolio_stock_gain_or_loss.data.stocks"},
{"$project":{"openStock":"$portfolio_stock_gain_or_loss.data.stocks.open","data":"$portfolio_stock_gain_or_loss.data"}},
{"$match": {"openStock.returns_rs":{"$gt":1000}}} ]).pretty()
I am doing this query
db.analytics.aggregate([
{
$match: {"event":"USER_SENTIMENT"}
},
{ $group: {
_id: {brand:"$data.brandId",sentiment:"$data.sentiment"},
count: {$sum : 1}
}
},
{ $group: {
_id: "$_id.brand",
sentiments: {$addToSet : {sentiment:"$_id.sentiment", count:"$count"}}
}
}
])
Which generates that :
{
"result" : [
{
"_id" : 57,
"sentiments" : [
{
"sentiment" : "Meh",
"count" : 4
}
]
},
{
"_id" : 376,
"sentiments" : [
{
"sentiment" : "Meh",
"count" : 1
},
{
"sentiment" : "Happy",
"count" : 1
},
{
"sentiment" : "Confused",
"count" : 1
}
]
}
],
"ok" : 1
}
But What I want is that :
[
{
"_id" : 57,
"Meh" : 4
},
{
"_id" : 376,
"Meh" : 1,
"Happy" : 1,
"Confused" : 1
}
]
Any idea on how to transform that? The blocking point for me is to transform a value into a key.
I have a collections
users
{
"_id" : ObjectId("53738eb7ac8ee07007c1d75a"),
"first" : "Shivam",
"connections" : [
{
"invit_made" : [ ],
"fl_no" : 615,
"date" : ISODate("2014-05-16T00:00:00Z"),
"fl" : "LB",
"TYP" : "ZLP",
"invit_reciv" : [ ],
},
{
"invit_made" : [ ],
"fl_no" : 615,
"date" : ISODate("2014-05-20T00:00:00Z"),
"fl" : "LB",
"invit_reciv" : [ ],
"TYP" : "ZLP",
}
]
}
I am performing update accoding to date in connections.but wrong nested documented is updated in my case.
db.users.update(
{ 'connections.TYP' : 'ZLP'
,'connections.fl' : 'LB'
,'connections.date' : ISODate("2014-05-20T00:00:00Z")
},
{ '$addToSet' : {
'connections.$.invit_reciv' : { 'last' : 'abc'}
}
})
Actual Result I am getting.
{
"_id" : ObjectId("53738eb7ac8ee07007c1d75a"),
"first" : "Shivam",
"connections" : [
{
"invit_made" : [ ],
"fl_no" : 615,
"date" : ISODate("2014-05-16T00:00:00Z"),
"fl" : "LB",
"TYP" : "ZLP",
"invit_reciv" : [
{
"last" : "abc"
}
],
},
{
"invit_made" : [ ],
"fl_no" : 615,
"date" : ISODate("2014-05-20T00:00:00Z"),
"fl" : "LB",
"invit_reciv" : [ ],
"TYP" : "ZLP",
}
]
}
Result I expect
{
"_id" : ObjectId("53738eb7ac8ee07007c1d75a"),
"first" : "Shivam",
"connections" : [
{
"invit_made" : [ ],
"fl_no" : 615,
"date" : ISODate("2014-05-16T00:00:00Z"),
"fl" : "LB",
"TYP" : "ZLP",
"invit_reciv" : [ ],
},
{
"invit_made" : [ ],
"fl_no" : 615,
"date" : ISODate("2014-05-20T00:00:00Z"),
"fl" : "LB",
"invit_reciv" : [
{
"last" : "abc"
}
],
"TYP" : "ZLP",
}
]
}
Please help me understand what is happening in present query and what is wrong with it.
You should use $elemMatch with positional operator, it allows you to match specific nested doc:
db.users.update(
{ connections:{
$elemMatch:{
'TYP' : 'ZLP',
'fl' : 'LB',
'date' : ISODate("2014-05-20T00:00:00Z")
}
}
},
{ '$addToSet' : {
'connections.$.invit_reciv' :
{ 'last' : 'abc'}
}
}
)