简体   繁体   中英

How to dynamically fetch data from mongodb

I'm looking to get the values dynamically from the database using NodeJS
What i am doing now is that i am taking the values hard-coded.

Note :
$text : The collection index.

 db.collection('Questions').find({$text: {$search: typed}}, {projection: { _id: 0, "How": 1, "Where": 1, "Who Approves": 1, "Applied Extra": 1, "Inform Personally": 1, "Pre Planning": 1, "Types": 1}}).toArray(function(err, result) {
        if (err)
            console.log(err);

        if (typed.trim() == "How To Apply") {
            var responding_how = JSON.stringify(result).substring(JSON.stringify(result).indexOf("How") + 6, JSON.stringify(result).indexOf("You Want Off On.") + 15)
            console.log("How To Apply - " + responding_how);
            res.send(responding_how);
        }
        if (typed.trim() == "Where To Apply") {
            var responding_where = JSON.stringify(result).substring(JSON.stringify(result).indexOf("https://"), JSON.stringify(result).indexOf("Who Approves") - 3)
            console.log("Where To Apply - " + responding_where);
            res.send("<a target='_blank' href=" + responding_where + ">Apply off</a>");
        }

Make a new collection , make it hold all your "headers" , you can call it Projections. Query that collection (find({})). Send the quert response (after exception-check) to the Questions "find" query :

db.collection('Questions').find({$text: {$search: typed}},ProjectionsResponse 
}).toArray(...
...)  

I hope this is what you meant..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM