3

So I want to find a specific document by id so for example

[
{
"_id": "5d0381ad681a2a3aa1dc5872",
"role_num": 1,
"first_name": "Adilf",
"last_name": "Adli",
"marks": 97
},
{
"_id": "5d0381ad681a2a3aa1dc5873",
"role_num": 2,
"first_name": "Mario",
"last_name": "Adl",
"marks": 93
},
{
"_id": "5d0381ad681a2a3aa1dc5874",
"role_num": 3,
"first_name": "Adi",
"last_name": "Ad",
"marks": 89
},
{
"_id": "5d0381ad681a2a3aa1dc5875",
"role_num": 4,
"first_name": "Harold",
"last_name": "Nome",
"marks": 78
},
{
"_id": "5d10b6124adac42c4467cb88",
"role_num": 6,
"first_name": "Mohammed",
"last_name": "Khan",
"marks": 91
},
{
"_id": "5d10b65f4adac42c4467cb89",
"role_num": 6,
"first_name": "Mohammed",
"last_name": "Khan",
"marks": 91
},
{
"_id": "5d10ebc562562921883741a1",
"role_num": 6,
"first_name": "Mohammed",
"last_name": "Khan",
"marks": 91
},
{
"_id": "5d11af3a62562921883741a3",
"role_num": 6,
"first_name": "Mohammed",
"last_name": "Khan",
"marks": 91
},
{
"_id": "5d11b40b62562921883741a4",
"role_num": 6,
"first_name": "Mohammed",
"last_name": "Khan",
"marks": 91
},
{
"_id": "5d11b4cd6dc8db0fe0f489fc",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d11b5131beb7f338414bc96",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d11b56cd0baff00cc616ebb",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d11b586d76c463d30d02355",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d11b74effe56935d88d910e",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d11b7d1e425ab1dbceda262",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d11b90e44b2c61650377951",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d120883a2f4d83bac8441c7",
"first_name": "Maria"
},
{
"_id": "5d1208cca2f4d83bac8441c8",
"first_name": "Maria"
},
{
"_id": "5d12093fa2f4d83bac8441c9",
"first_name": "Maria"
},
{
"_id": "5d1209c0a2f4d83bac8441ca",
"first_name": "Baki"
},
{
"_id": "5d120f0ed9e07909a413cd85",
"role_num": "5",
"first_name": "Arojfsdof",
"last_name": "adfjaf",
"marks": "12"
},
{
"_id": "5d12fff7d9e07909a413cd8a",
"role_num": "17",
"first_name": "BARKS",
"last_name": "DUDUE",
"marks": "45"
},
{
"_id": "5d131c42313e183ae09966c3",
"role_num": "4",
"first_name": "Maam",
"last_name": "Alsad",
"marks": "80"
}
]

Above I have a list of students I want to do a get method to find a specific one by id for example lets say I want to find this

{
    "_id": "5d131c42313e183ae09966c3",
    "role_num": "4",
    "first_name": "Maam",
    "last_name": "Alsad",
    "marks": "80"
}

and here is my code for the find id let me know what I have to fix

second.get('/students/:id', (req, res) => {
   db.collection('students').find({"_id": ObjectID(req.body._id)}).toArray((err,result) => {
     if(!err) {
        res.send(result);
     } else {
         console.log(err);
     }

   });
});

Before I was doing a findOne method but that was used to find only the occurance of the first doucment which wasn't what I was looking for

Arnald
  • 43
  • 3
  • 9
  • @franiis so in my code if I want to find the document of { "_id": "5d0381ad681a2a3aa1dc5874", "role_num": 3, "first_name": "Adi", "last_name": "Ad", "marks": 89 }, – Arnald Jun 26 '19 at 10:19
  • @franiis what can I change in my code and I do not want to hard code the values – Arnald Jun 26 '19 at 10:20
  • I'm not Node.js developer. Quering database by `id` is the most important task in database communication. Maybe this: https://stackoverflow.com/questions/12769252/querying-a-mongodb-based-on-mongo-id-in-a-node-js-app (I'm sure there are some examples somewhere). – franiis Jun 26 '19 at 10:53

3 Answers3

10

There is new missing

var ObjectID = require('mongodb').ObjectID;   

db.collection('students').find({"_id": new ObjectID(req.body._id)})
helvete
  • 2,455
  • 13
  • 33
  • 37
Puser
  • 121
  • 1
  • 7
1

The accepted answer is not a valid syntax, the correct syntax is:

var ObjectId = require('mongodb').ObjectId;
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Asad Madni
  • 11
  • 1
-2

To fetch a record from Mongo with it's ID, db.collection('students').findOne({"_id:": id}) is the correct format. You'll only ever fetch one record, as you can't have 2 records with the same ID.

H Aßdøµ
  • 2,925
  • 4
  • 26
  • 37
NadavM
  • 35
  • 7
  • where do I define id like "_id": id – Arnald Jun 26 '19 at 12:13
  • Mine is part of an async function: `let client = await MongoClient.connect(uri, {useNewUrlParser: true}); let db = client.db('students'); try { const res = await db.collection('students').findOne({"_id": id}); // do something with response ` – NadavM Jun 26 '19 at 14:09
  • Same can be done without async: `db.collection('students').findOne({"id": id}, function(err, result) {//handle result and error})` – NadavM Jun 26 '19 at 14:12