sql >> Base de Datos >  >> NoSQL >> MongoDB

Seleccionar campos anidados en mongo db

Debe agregar de la siguiente manera:

  • Construir un find objeto para que coincida solo con los registros que contienen ($ existe) el idioma.
  • Construir una Projection objeto para proyectar los campos.

Código:

var currentLang = "en";
var project = {};
project["title"] = "$"+currentLang+".title";
project["content"] = "$"+currentLang+".content";
project["images"] = 1;

var find = {};
find[currentLang] = {"$exists":true};

db.collection.aggregate([
{$match:find},
{$project:project}
])