BlackdestinyXX
BlackdestinyXX

Reputation: 356

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!

Upvotes: 3

Views: 203

Answers (1)

Đăng Khoa Đinh
Đăng Khoa Đinh

Reputation: 5411

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

Upvotes: 3

Related Questions