I have a mongo db with this model:
_id: ObjectId("5705005b240166e927f841cb")
chapters: {
type: Array,
default: [
{
"id":"capitulo_0",
"active": true,
"title": "CAPÍTULO 0 - INTRODUCCIÓN",
"sections": [
{
"title": "Institucional",
"type": "Video",
"id": "d74fb24654a2",
"url": "jPTG5P0528k",
"active": true
}
]
},
{
"id":"capitulo_1",
"active": false,
"title": "CAPÍTULO 1 - BIENVENIDA",
"sections": [
{
"title": "Introducción",
"type": "Video",
"url": "j2TG1P05k8k",
"id": "b2454d7f66de",
"active": false
}
]
},
...
]
}
For the query i have the user_id and the id of the sections and i need update the active field of the sections array.
I'm doing this:
User.findOneAndUpdate({_id: userId, 'chapters.sections':{$elemMatch: {id:sectionId}}}, {$set: {'sections.$.active': false}}).exec(function (err, doc) {console.log(doc)});
The active field not change.
How can I do it this query?
Thank's
I dont have your data so run this:
User.findOne({_id: userId, 'chapters.sections':{$elemMatch: {id:sectionId}}})
and see if you get response and record will be found, because your update looks fine.
UPDATE:
After seeing your data I think you are missing chapters in your query
User.findOneAndUpdate({_id: userId, 'chapters.sections':{$elemMatch: {id:sectionId}}}, {$set: {'chapters.sections.$.active': false}}).exec(function (err, doc) {console.log(doc)});
I hope this helps
Related
I have a collection with the following documents (for example):
{
"_id": {
"$oid": "61acefe999e03b9324czzzzz"
},
"matchId": {
"$oid": "61a392cc54e3752cc71zzzzz"
},
"logs": [
{
"actionType": "CREATE",
"data": {
"talent": {
"talentId": "qq",
"talentVersion": "2.10",
"firstName": "Joelle",
"lastName": "Doe",
"socialLinks": [
{
"type": "FACEBOOK",
"url": "https://www.facebook.com"
},
{
"type": "LINKEDIN",
"url": "https://www.linkedin.com"
}
],
"webResults": [
{
"type": "VIDEO",
"date": "2021-11-28T14:31:40.728Z",
"link": "http://placeimg.com/640/480",
"title": "Et necessitatibus",
"platform": "Repellendus"
}
]
},
"createdBy": "DEVELOPER"
}
},
{
"actionType": "UPDATE",
"data": {
"talent": {
"firstName": "Joelle new",
"webResults": [
{
"type": "VIDEO",
"date": "2021-11-28T14:31:40.728Z",
"link": "http://placeimg.com/640/480",
"title": "Et necessitatibus",
"platform": "Repellendus"
}
]
}
}
}
]
},
{
"_id": {
"$oid": "61acefe999e03b9324caaaaa"
},
"matchId": {
"$oid": "61a392cc54e3752cc71zzzzz"
},
"logs": [....]
}
a brief breakdown: I have many objects like this one in the collection. they are a kind of an audit log for actions takes on other documents, 'Match(es)'. for example CREATE + the data, UPDATE + the data, etc.
As you can see, logs field of the document is an array of objects, each describing one of these actions.
data for each action may or may not contain specific fields, that in turn can also be an array of objects: socialLinks and webResults.
I'm trying to remove sensitive data from all of these documents with specified Match ids.
For each document, I want to go over the logs array field, and change the value of specific fields only if they exist, for example: change firstName to *****, same for lastName, if those appear. also, go over the socialLinks array if exists, and for each element inside it, if a field url exists, change it to ***** as well.
What I've tried so far are many minor variations for this query:
$set: {
'logs.$[].data.talent.socialLinks.$[].url': '*****',
'logs.$[].data.talent.webResults.$[].link': '*****',
'logs.$[].data.talent.webResults.$[].title': '*****',
'logs.$[].data.talent.firstName': '*****',
'logs.$[].data.talent.lastName': '*****',
},
and some play around with this kind of aggregation query:
[{
$set: {
'talent.socialLinks.$[el].url': {
$cond: [{ $ne: ['el.url', null] },'*****', undefined],
},
},
}]
resulting in errors like: message: "The path 'logs.0.data.talent.socialLinks' must exist in the document in order to apply array updates.",
But I just cant get it to work... :(
Would love an explanation on how to exactly achieve this kind of set-only-if-exists behaviour.
A working example would also be much appreciated, thx.
Would suggest using $\[<indentifier>\] (filtered positional operator) and arrayFilters to update the nested document(s) in the array field.
In arrayFilters, with $exists to check the existence of the certain document which matches the condition and to be updated.
db.collection.update({},
{
$set: {
"logs.$[a].data.talent.socialLinks.$[].url": "*****",
"logs.$[b].data.talent.webResults.$[].link": "*****",
"logs.$[b].data.talent.webResults.$[].title": "*****",
"logs.$[c].data.talent.firstName": "*****",
"logs.$[d].data.talent.lastName": "*****",
}
},
{
arrayFilters: [
{
"a.data.talent.socialLinks": {
$exists: true
}
},
{
"b.data.talent.webResults": {
$exists: true
}
},
{
"c.data.talent.firstName": {
$exists: true
}
},
{
"d.data.talent.lastName": {
$exists: true
}
}
]
})
Sample Mongo Playground
Here i want to update a given document as below
{
"emailid": "xxx.gmail",
"user_devices": [],
"devices": [{
"created_time": 1607153351,
"token": 123
},
{
"created_time": 1807153371,
"token": 1345
}]
}
here i need to update field devices with the token of 1345 with a new field like "Newfield":"newfield" where the final output would look like as below
{
"emailid": "xxx.gmail",
"user_devices": [],
"devices": [{
"created_time": 1607153351,
"token": 123
},
{
"created_time": 1807153371,
"token": 1345,
"Newfield":"newfield"
}]
}
How to update the mongo db like this. Thanks in advance for your answers.
db.products.update({
"devices.token": 1345 //Matching condition
},
{
$set: {
"devices.$.t": 2 //Updates the matched element in the array
}
})
you can do it using positional operator - Reference
I'm trying to execute a query like:
{array.0.property: {$ne: null}}.
It return nothing even if all documents have this property different from null.
After some tests i noticed that it work using $elemMatch, but i need to query only for the first element of the array.
The first element is to be considered as "Master" where all query should search.
I can't change document "schema".
Anyone know ho to solve this problem?
I'm using Mongodb 3.6.8.
Thanks in advice.
Example query:
db.getCollection('tasks').find({'details.0.code': {$ne: null}});
Example documents:
{
"name": "test",
"date": 2018-07-17 06:30:00.000Z,
.....,
"details": [
{
"code": '123',
"description": 'something',
"resolutionYear": 2018
},
{
"code": null,
"description": 'secondary',
"resolutionYear": 2019
}
]
},
{
"name": "exam",
"date": 2018-09-20 09:00:00.000Z,
.....,
"details": [
{
"code": null,
"description": 'exam',
"resolutionYear": null
}
]
}
In MongoDb, with a Projection, I want to remove an array that contains one empty object [{}] resulting from a $unwind preserveNullAndEmptyArrays & group.
[{
"title": "Papaye",
"childrens": [{}],
"parents": [{
"title": "Arbres fruitiers",
"url": "/documents/plantes/arboriculture/arbres-fruitiers"
}
],
"url": "/documents/plantes/arboriculture/arbres-fruitiers/papaye"
},
{
"title": "Arbres fruitiers",
"childrens": [{
"title": "Tavelure",
"url": "/documents/maladies/tavelure"
},
{
"title": "Longane",
"url": "/documents/plantes/arboriculture/arbres-fruitiers/longane"
}],
"parents": [{
"title": "Arboriculture",
"url": "/documents/plantes/arboriculture"
}],
"url": "/documents/plantes/arboriculture/arbres-fruitiers"
}
Pipeline like :
var pipeline = [];
pipeline.push({$match:{url:/^\//}});
(...)
var proj = {};
proj.title = true;
proj.parents = true;
proj.url = true;
proj.parents = ???
proj.childrens = ???
pipeline.push({$project:proj});
db.getCollection('Pages').aggregate(pipeline)
Thanks in advance
Ok, I found the solution. Just test if url is null for the first array entry.
proj.childrens = {
"$cond", [
"$eq", [
"$arrayElemAt",["$childrens.url",0],
null
],
[],
"$childrens"
]
};
This is my document in MongoDB:
{
"_id": {
"$oid": "566193b0c9b5290f234242"
},
"name": "fake-name-1",
"profiles": [
{
"real-name": "fake-name-1",
"color": "fake-color-1"
},
{
"real-name": "fake-name-2",
"color": "fake-color-2",
"active": true
},
{
"real-name": "fake-name-3",
"color": "fake-color-3"
}
]
}
I'm real newbie to MondoDb, and are trying to find the document where profiles contains a real-name with "MArtin43221" and active = true.
How do I create a search query for this?
I've tried:
{"profiles": ["real-name":"MArtin43221", "active":true]}
Try elemMatch:
db.collection.find({"profiles": {$elemMatch:{"real-name":"MArtin43221", "active":true}}})