Update multiple subdocument without exact sub document level condition - mongodb

I have collection with the following document:
{
"_id" : ObjectId("56cbf9cd75de3ee9057b23c7"),
"title" : "Abc",
"comments" : [
{
"_id" : "",
"message" : "",
"active" : 1,
"status" : 1,
},
{
"_id" : "",
"message" : "",
"active" : 0,
"status" : 1,
},
{
"_id" : "",
"message" : "",
"active" : 1,
"status" : 1,
}
],
}
{
"_id" : ObjectId("553744ae75de3eb9128b4568"),
"title" : "Jhon",
"comments" : [
{
"_id" : "",
"message" : "",
"active" : 1,
"status" : 1,
}
]
}
I need to update all status of comments as 0 where comments.active=1. I tried to solve it as below, but only first record of the comments is updated. Please help me..
db.comments.update({'comments.active':1},{$set:{'comments.$.status':0}})

Per this issue New operator to update all matching items in an array, currently there is no operation to do that in mongodb. Feel so sad, this issue lasts for 6 years.
There could be one work around in mongo shell as below.
> db.comments
.find({})
.forEach(function(doc) {
doc.comments.map(function(c) {
if (c.active == 1) {
c.status = 0;
}
});
db.comments.update(
{_id: doc._id},
{$set: {comments: doc.comments}});
});

Related

Mongo db query for multiple conditions

I have a Mongodb Json which look like this
{
"_id" : "5b862ebecebe455a1744",
"userId" : "111",
"courses" : [
{
"stateName" : "statge 1",
"courseId" : "1453",
"courseName" : "Program Training 1",
"duration" : 1,
"lag" : 0,
"courseType" : "1",
"transitionType" : "onComplete",
"scheduledStartDate" : ISODate("2018-07-27T16:23:14.000+05:30"),
"scheduledEndDate" : ISODate("2018-07-27T16:23:14.000+05:30"),
"courseProgress" : 0,
"ASD" : ISODate("2018-09-17T23:18:30.636+05:30"),
"score" : 0
},
{
"stateName" : "stage 2",
"courseId" : "1454",
"courseName" : "Program Assessment 1",
"duration" : 1,
"lag" : 0,
"courseType" : "2",
"transitionType" : "onComplete",
"scheduledStartDate" : ISODate("2018-07-28T16:23:14.000+05:30"),
"scheduledEndDate" : ISODate("2018-07-28T16:23:14.000+05:30"),
"courseProgress" : 0,
"score" : 0
},
{
"stateName" : "stage 3",
"courseId" : "911",
"courseName" : "Program Training 3",
"duration" : 1,
"lag" : 0,
"courseType" : "1",
"transitionType" : "onComplete",
"scheduledStartDate" : ISODate("2018-07-29T16:23:14.000+05:30"),
"scheduledEndDate" : ISODate("2018-07-29T16:23:14.000+05:30"),
"courseProgress" : 0,
"score" : 0
}
],
"userStatus" : 1,
"modified" : ISODate("2018-09-12T11:49:47.400+05:30"),
"created" : ISODate("2018-09-12T11:49:47.400+05:30"),
"completionStatus" : "IP",
"currentState" : {
"courseProgress" : 0,
"stateName" : "statge 1",
"courseId" : "1453",
"courseName" : "Program Training 1"
}
}
I want to find a query where condition is. Please help, as I am new to mongodb
courses.transitionType = oncomplete
(PROGRESS<100||(PROGRESS==100&&ASD exists false))
And print Result something like this which contain these below data
{
"_id" : "5b862ebecebe455a1744",
"courseData" : {
"userId" : "4688",
"courseId" : "1476",
"courseProgress" : 0
}
}
You will have to use an aggregation with a $match stage and a $project to format your result.
The tricky part of your request is that you want an answer by course, but 1 item of your collection contains many courses. So first, you can use the $unwind stage to separate every course
db.[CollectionName].aggregate([
{
$unwind : '$courses'
}
{
$match: {
'courses.transitionType': 'onComplete',
$or: [
{
'courses.courseProgress': { $lt: 100 }
},
{
'courses.courseProgress': 100,
ASD: { $exists: 0 }
}
]
}
},
{
$project: {
_id: '0',
courseData: {
userId: '$courses.userId',
courseId: '$courses.courseId',
courseProgress: '$courses.courseProgress'
}
}

Returning only sub document based on subdocument _id which is the items of array field of the mongodb document [duplicate]

This question already has answers here:
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Closed 4 years ago.
My schema goes like this
[
{
"_id" : ObjectId("565f0f5d77f0c7bd11bbadd8"),
"blog_id" : ObjectId("56587befdb7224110f007233"),
"comments" : [
{
"user_id" : ObjectId("562fa014888806820e21e0df"),
"user_full_name" : "Niroj Paudel",
"comment" : "pradip is bhole baba",
"_id" : ObjectId("565f0f5d77f0c7bd11bbadd9"),
"dt" : ISODate("2015-12-02T15:33:49.578Z")
},
{
"user_id" : ObjectId("562fa014888806820e21e0df"),
"user_full_name" : "Niroj Paudel",
"comment" : "honkog pokhara... he he ha ha",
"_id" : ObjectId("565f1034fd07cbfc1129db0b"),
"dt" : ISODate("2015-12-02T15:37:24.581Z")
}
],
"record_count" : 2,
"__v" : 0
}]
What I am trying is to select particular item of comments array based on the blog_id and comments._id ..... but instead of returning only the particular comment it is returning the whole document .
Currently I have following query
db.blog_comments..findOne({
"blog_id" : ObjectId("56587befdb7224110f007233"),
"comments._id":ObjectId("565f1034fd07cbfc1129db0b")
})
This query is returning the whole document i.e.-
[
{
"_id" : ObjectId("565f0f5d77f0c7bd11bbadd8"),
"blog_id" : ObjectId("56587befdb7224110f007233"),
"comments" : [
{
"user_id" : ObjectId("562fa014888806820e21e0df"),
"user_full_name" : "Niroj Paudel",
"comment" : "pradip is bhole baba",
"_id" : ObjectId("565f0f5d77f0c7bd11bbadd9"),
"dt" : ISODate("2015-12-02T15:33:49.578Z")
},
{
"user_id" : ObjectId("562fa014888806820e21e0df"),
"user_full_name" : "Niroj Paudel",
"comment" : "honkog pokhara... he he ha ha",
"_id" : ObjectId("565f1034fd07cbfc1129db0b"),
"dt" : ISODate("2015-12-02T15:37:24.581Z")
}
],
"record_count" : 2,
"__v" : 0
}]
but I want just this
[{
"user_id" : ObjectId("562fa014888806820e21e0df"),
"user_full_name" : "Niroj Paudel",
"comment" : "honkog pokhara... he he ha ha",
"_id" : ObjectId("565f1034fd07cbfc1129db0b"),
"dt" : ISODate("2015-12-02T15:37:24.581Z")
}]
What I am missing please suggest..
Thank you
you can use $elemMatch in your projection:
test> db.project_sub.findOne({
... "blog_id" : ObjectId("56587befdb7224110f007233")
... },{
... "comments": { $elemMatch: { _id: ObjectId("565f1034fd07cbfc1129db0b") } }
... })
{
"_id": ObjectId("565f0f5d77f0c7bd11bbadd8"),
"comments": [
{
"user_id": ObjectId("562fa014888806820e21e0df"),
"user_full_name": "Niroj Paudel",
"comment": "honkog pokhara... he he ha ha",
"_id": ObjectId("565f1034fd07cbfc1129db0b"),
"dt": ISODate("2015-12-02T15:37:24.581Z")
}
]
}

Get document based on multiple criteria of embedded collection

I have the following document, I need to search for multiple items from the embedded collection"items".
Here's an example of a single SKU
db.sku.findOne()
{
"_id" : NumberLong(1192),
"description" : "Uploaded via CSV",
"items" : [
{
"_id" : NumberLong(2),
"category" : DBRef("category", NumberLong(1)),
"description" : "840 tag visual",
"name" : "840 Visual Mini Round",
"version" : NumberLong(0)
},
{
"_id" : NumberLong(7),
"category" : DBRef("category", NumberLong(2)),
"description" : "Maxi",
"name" : "Maxi",
"version" : NumberLong(0)
},
{
"_id" : NumberLong(11),
"category" : DBRef("category", NumberLong(3)),
"description" : "Button",
"name" : "Button",
"version" : NumberLong(0)
},
{
"_id" : NumberLong(16),
"category" : DBRef("category", NumberLong(4)),
"customizationFields" : [
{
"_class" : "CustomizationField",
"_id" : NumberLong(1),
"displayText" : "Custom Print 1",
"fieldName" : "customPrint1",
"listOrder" : 1,
"maxInputLength" : 12,
"required" : false,
"version" : NumberLong(0)
},
{
"_class" : "CustomizationField",
"_id" : NumberLong(2),
"displayText" : "Custom Print 2",
"fieldName" : "customPrint2",
"listOrder" : 2,
"maxInputLength" : 17,
"required" : false,
"version" : NumberLong(0)
}
],
"description" : "2 custom lines of farm print",
"name" : "Custom 2",
"version" : NumberLong(2)
},
{
"_id" : NumberLong(20),
"category" : DBRef("category", NumberLong(5)),
"description" : "Color Red",
"name" : "Red",
"version" : NumberLong(0)
}
],
"skuCode" : "NF-USDA-XC2/SM-BC-R",
"version" : 0,
"webCowOptions" : "840miniwithcust2"
}
There are repeat items.id throughout the embedded collection. Each Sku is made up of multiple items, all combinations are unique, but one item will be part of many Skus.
I'm struggling with the query structure to get what I'm looking for.
Here are a few things I have tried:
db.sku.find({'items._id':2},{'items._id':7})
That one only returns items with the id of 7
db.sku.find({items:{$all:[{_id:5}]}})
That one doesn't return anything, but it came up when looking for solutions. I found about it in the MongoDB manual
Here's an example of a expected result:
sku:{ "_id" : NumberLong(1013),
"items" : [ { "_id" : NumberLong(5) },
{ "_id" : NumberLong(7) },
{ "_id" : NumberLong(12) },
{ "_id" : NumberLong(16) },
{ "_id" :NumberLong(2) } ] },
sku:
{ "_id" : NumberLong(1014),
"items" : [ { "_id" : NumberLong(5) },
{ "_id" : NumberLong(7) },
{ "_id" : NumberLong(2) },
{ "_id" : NumberLong(16) },
{ "_id" :NumberLong(24) } ] },
sku:
{ "_id" : NumberLong(1015),
"items" : [ { "_id" : NumberLong(5) },
{ "_id" : NumberLong(7) },
{ "_id" : NumberLong(12) },
{ "_id" : NumberLong(2) },
{ "_id" :NumberLong(5) } ] }
Each Sku that comes back has both a item of id:7, and id:2, with any other items they have.
To further clarify, my purpose is to determine how many remaining combinations exist after entering the first couple of items.
Basically a customer will start specifying items, and we'll weed it down to the remaining valid combinations. So Sku.items[0].id=5 can only be combined with items[1].id=7 or items[1].id=10 …. Then items[1].id=7 can only be combined with items[2].id=20 … and so forth
The goal was to simplify my rules for purchase, and drive it all from the Sku codes. I don't know if I dug a deeper hole instead.
Thank you,
On the part of extracting the sku with item IDs 2 and 7, when I recall correctly, you have to use $elemMatch:
db.sku.find({'items' :{ '$all' :[{ '$elemMatch':{ '_id' : 2 }},{'$elemMatch': { '_id' : 7 }}]}} )
which selects all sku where there is each an item with _id 2 and 7.
You can use aggregation pipelines
db.sku.aggregate([
{"$unwind": "$sku.items"},
{"$group": {"_id": "$_id", "items": {"$addToSet":{"_id": "$items._id"}}}},
{"$match": {"items._id": {$all:[2,7]}}}
])

MongoDB ReduceMap An Array Property that Contains Another Array

I'm still new to MongoDB and non-relational databases in general so if the answer to my question is "Dude, you're thinking about your data in the wrong way", please let me know.
That being said, here's what I'm after. I have the following data:
{
"_id" : ObjectId("549b26b370b452eeb6acecd3"),
"user" : "54530e03c575dc86d61d22f8",
"workHistory" : [
{
"description" : "",
"endDate" : null,
"name" : "My Company",
"skills" : [
{
"_id" : ObjectId("549b29c970b452eeb6acecd4")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecd5")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecda")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecdb")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecdd")
},
{
"_id" : ObjectId("549b29c970b452eeb6acece9")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecea")
},
{
"_id" : ObjectId("549b995b70b452eeb6acecf9")
},
{
"_id" : ObjectId("549b999470b452eeb6acecfa")
},
{
"_id" : ObjectId("549b9ab670b452eeb6acecfb")
}
],
"startDate" : "2013-10-01"
},
{
"description" : "",
"endDate" : "2013-10-01",
"name" : "Another Company",
"skills" : [
{
"_id" : ObjectId("549b29c970b452eeb6acecd4")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecd5")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecdb")
},
{
"_id" : ObjectId("549b29c970b452eeb6acecdd")
},
{
"_id" : ObjectId("549b29c970b452eeb6acece1")
},
{
"_id" : ObjectId("549b29c970b452eeb6acece9")
},
{
"_id" : ObjectId("549b995b70b452eeb6acecf9")
}
],
"startDate" : "2012-04-01"
},
.....
What I am trying to do is resolve that data I can list the actual skill names under "skills" instead of just object ID's. For reference here's the skills collection:
/* 0 */
{
"_id" : ObjectId("549b29c970b452eeb6acecd4"),
"name" : "CSS",
"description" : ""
}
/* 1 */
{
"_id" : ObjectId("549b29c970b452eeb6acecd5"),
"name" : "HTML5",
"description" : ""
}
/* 2 */
{
"_id" : ObjectId("549b29c970b452eeb6acecd6"),
"name" : "Ruby",
"description" : ""
}
/* 3 */
{
"_id" : ObjectId("549b29c970b452eeb6acecd7"),
"name" : "Ruby on Rails",
"description" : ""
}
I've tried looking into using MapReduce to do some kind of join, but I can barely wrap my head around it and all the examples I'm finding don't have data this complex.
What's the best approach for getting my desired result? Side note: I'm not 100% sure that simply embedding the skills in the "skills" property is the right approach. I'm trying to keep them separate because the skills may be listed in other areas of my application separate from work history.

MongoDB show not all elements in subdocument

I have the document of the following structure:
{
"_id" : ObjectId("50b8f881065f90c025000014"),
"education" : {
"schoolCountry" : 4,
"schoolTown" : -1,
"uniCountry" : 4,
"uniTown" : -1
},
"info" : {
"ava" : "auto.jpg",
"birthday" : ISODate("1942-04-01T21:00:00Z"),
"email" : "mail#gmail.com",
"name" : "name",
"sex" : 1,
"surname" : "surname"
}
}
I am trying to output only surname and name
The only thing I was able to achieve is this:
db.COLL.find({ }, {
"_id" : 0,
"education" : 0,
"info" : 1
})
My idea to show only elements that I need from subdocument failed:
db.COLL.find({ }, {
"_id" : 0,
"education" : 0,
"info.surname" : 1,
"info.name" : 1,
})
But hidding (info.email : 0) works. Is it possible to achieve my goal without hidding all unneeded fields?
You can't mix including and excluding fields, aside from turning off _id (which is included by default).
So just request the info.surname and info.name fields:
db.coll.find({ }, {
"_id" : 0,
"info.surname" : 1,
"info.name" : 1,
})
Sample output:
{ "info" : { "name" : "name", "surname" : "surname" } }