user17065575
user17065575

Reputation: 9

How can I take value of the field from mongodb

I'm new to Node.js. I have a collection with four documents. All documents have three fields "_id", "num" and "str". I send the result on the page of browser

app.get('/', (req,res) => {
 hwUsers.aggregate(pipeline)
    .then((hwu) => {
        res.send(hwu);})
    .catch((err) => {
        res.send(err)});
});

This is my pipeline:

let pipeline = [{
    "$match": {num: 1}
    },
    {
    "$project":{str: 1, _id: 0}
}];

This is result which i see on the page [{"str":"lo "}] My question is next. How can I take just value from field, if I can of course.

Upvotes: 1

Views: 170

Answers (1)

Danik Garbazhii
Danik Garbazhii

Reputation: 46

You are getting the document(object) from your collection.The aggregation always returns an object or array of objects. So if you want to get the value you can type something like this:res.send(hwu.map(item=>{ return item.str })) And you will get the array of values.

Upvotes: 1

Related Questions