简体   繁体   中英

mongodb get entire document from a value

i want to get all values of a mongodb document from a single value.
Example:

    "id": "id",
    "name": "name",
    "description": "description",
    "invite": "invite",
    "support_server": "server",
    "developer": "developer",
    "avatar": null

This is my object.
When i use.findOne() function of MongoDB, i want to get all values. Example:

<collection>.findOne({"id":"id"})

//get all values name, description, invite ecc. from id

(i'm using node.js)

this is my current code:

   socket.on("bot_req_id", function(data) {
       let db = mongoose.db("wumpusCave")
       let bots = db.collection("bots")
           console.log(data)
           let bot = bots.findOne({data})
           console.log()
           socket.emit("bot_res_id", bot)
   })

How i can do this?
Thanks in advice and sorry for bad english!

When you call bots.findOne({data}) it returns a "Promise", not the data. You need to wait for the Promise resolve to get the data. You can try this code.

   socket.on("bot_req_id", async function(data) {
       let db = mongoose.db("wumpusCave")
       let bots = db.collection("bots")
           console.log(data)
           let bot = await bots.findOne({data})
           console.log(bot);
           socket.emit("bot_res_id", bot)
   })

This article may help you understand the concept: https://scotch.io/courses/10-need-to-know-javascript-concepts/callbacks-promises-and-async

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