Select a nested array object and replace it

I got an array (as a result of a mongoDB request) with some elements like this:

{ "_id": "ExxTDXJSwvRbLdtpg", "content": [ { "content": "First paragraph", "language":"en", "timestamp":1483978498 }, { "content": "Erster Abschnitt", "language":"de", "timestamp":1483978498 } ] } 

But I need to get only one content field for each element of the data array, which should be selected by the language. Thus, the result should be (when choosing English content):

 { "_id": "ExxTDXJSwvRbLdtpg", "content": "First paragraph" } 

instead of getting all the content data ...

I tried to do this with find(c => c.language === 'en) , but I don't know how to use this for all elements of the data array. Perhaps you can also get the data directly as a mongodb request ??

+5
source share
4 answers

You can iterate through the array and replace the value inside.

 var array = [{ _id: "ExxTDXJSwvRbLdtpg", content: [{ content: "First paragraph", language: "en", timestamp: 1483978498 }, { content: "Erster Abschnitt", language: "de", timestamp: 1483978498 }] }]; array.forEach(a => a.content = a.content.find(c => c.language === 'en').content); console.log(array); 

content Version

 var array = [{ _id: "ExxTDXJSwvRbLdtpg", content: [{ content: "First paragraph", language: "en", timestamp: 1483978498 }, { content: "Erster Abschnitt", language: "de", timestamp: 1483978498 }] }, { _id: "no_content" }, { _id: "no_english_translation", content: [{ content: "Premier lot", language: "fr", timestamp: 1483978498 }, { content: "Erster Abschnitt", language: "de", timestamp: 1483978498 }] }]; array.forEach(function (a) { var language; if (Array.isArray(a.content)) { language = a.content.find(c => c.language === 'en'); if (language) { a.content = language.content; } else { delete a.content; } } }); console.log(array); 
+3
source

Given that _id and language are input variables, you can use this aggregate command to get the expected result:

 db.collection.aggregate([{ $match: { _id: _id, } }, { $unwind: '$content' }, { $match: { 'content.language': language, } }, { $project: { _id: 1, content: '$content.content' } }]) 
+3
source
 var aobjs = [{ "_id": "ExxTDXJSwvRbLdtpg", "content": [ { "content": "First paragraph", "language":"en", "timestamp":1483978498 }, { "content": "Erster Abschnitt", "language":"de", "timestamp":1483978498 } ] }]; var result = aobjs.map(o => ({ id: o._id, content: o.content.find(c => c.language === 'en').content })); 

This returns an object for each with a single identifier and content. In this example, the result would be:

[{id: 'ExxTDXJSwvRbLdtpg', content: 'First paragraph'}]

+1
source

You can try a regular query with something like below.

Using elemMatch

 db.collection.find({"_id": ObjectId("ExxTDXJSwvRbLdtpg")}, { "content": { $elemMatch: { "language": "en" } } }); 

or

Using positional operator

 db.collection.find({"_id": ObjectId("ExxTDXJSwvRbLdtpg"), "content.language" : "en"}, { "content.$":1}); 
0
source

Source: https://habr.com/ru/post/1262839/


All Articles