Tuesday, 14 March 2017

Express MongoDB find() based on _id field

So In my express app, I am trying to find() based on my _id field.

See my MongoDB record below.

{
        "_id": {
            "$oid": "58c2a5bdf36d281631b3714a"
        },
        "title": "EntertheBadJah",
        "subTitle": "Lorem ipsum dolor",
        "thmbNailImg": "",
        "headerImg": "",
         ... BLAH BLAH BLAH

When I use .find( _id: id ),

My id param = 58c2a5bdf36d281631b3714a as an integer.

How would I convert / use this to get my MongoDB record?

Here is my Express call:

//GET ARTICLE
app.get('/api/article', (req, res) => {

    var id = req.query.id
    var article = [];

    db.collection('articles')
        .find( _id: id )
        .then(result => {
            article = articles.concat(result);
        }).then(() => {
            res.send(article);
        }).catch(e => {
            console.error(e);
        });

});

Any help or advice is appreciated. Thank you in advance.



via danjonescidtrix

No comments:

Post a Comment