Search code examples
mongodbmeteorminimongo

How to dynamically give the collection filed name in Meteor?


I have these codes now:

OneCollection.find({}, {fields: {'oneFiled.child1': 1}});
OneCollection.find({}, {fields: {'oneFiled.child2': 1}});

But I want to give a dynamically child filed name.

let childfield = "child1";
OneCollection.find({}, {fields: {'oneFiled.childfield': 1}});  // How to write this one?

How can I dynamically give the filed name? Thanks


Solution

  • Like this ?

    let childField = 'child1';
    let listFields = {};
    listFields['oneField.'+childField]=1;
    OneCollection.find({}, {fields: listFields });