5

I got an array (as result of a mongoDB query) 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 just a single content field for each data array element, which should be selected by the language. So the result should be (assuming selecting the english content):

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

instead of getting all the content data...

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

0

3 Answers 3

3

You could iterate 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);

Version with check for content

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Would this work for documents with missing content field? Because not every element has a content field.
what should happen then?
3

Given that _id and language are input variables, then you could 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'
  }
}])

2 Comments

I'm using meteor and I think aggregate won't work with that :-(
oh, I am a Meteor dev too :) you could use aggregation in server with this package meteorhacks:aggregate
1
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 just id and content. In this example, result would be:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.