MongoDB
 sql >> Datenbank >  >> NoSQL >> MongoDB

Gibt nur den Array-Wert in der Mongo-Projektion zurück

JSON erlaubt nicht, dass die oberste Ebene ein Array ist, daher lässt eine normale Abfrage dies nicht zu. Sie können dies jedoch mit dem Aggregationsframework tun:

> db.test.remove();
> db.test.insert({ name: "Andrew", attributes: [ { title: "Happy"}, { title: "Sad" } ] });
> foo = db.test.aggregate( { $match: { name: "Andrew" } }, { $unwind: "$attributes" }, { $project: { _id: 0, title: "$attributes.title" } } );
{
    "result" : [
        {
            "title" : "Happy"
        },
        {
            "title" : "Sad"
        }
    ],
    "ok" : 1
}
> foo.result
[ { "title" : "Happy" }, { "title" : "Sad" } ]

Dies erzeugt jedoch kein Cursor-Objekt, was find tut.