ดังนั้นในแอปด่วนของฉัน ฉันกำลังพยายามค้นหา() ตามฟิลด์ _id ของฉัน
ดูบันทึก MongoDB ของฉันด้านล่าง
{
"_id": {
"$oid": "58c2a5bdf36d281631b3714a"
},
"title": "EntertheBadJah",
"subTitle": "Lorem ipsum dolor",
"thmbNailImg": "",
"headerImg": "",
... BLAH BLAH BLAH
เมื่อฉันใช้ .find( _id: id )
พารามิเตอร์ id
ของฉัน = 58c2a5bdf36d281631b3714a
ฉันจะแปลง / ใช้สิ่งนี้เพื่อรับบันทึก MongoDB ของฉันได้อย่างไร
นี่คือการโทรด่วนของฉัน:
//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);
});
});
ความช่วยเหลือหรือคำแนะนำใด ๆ ที่ชื่นชม ขอบคุณล่วงหน้า.
แก้ไข: (คำค้นหาที่แก้ไขของฉัน)
//GET ARTICLE
app.get('/api/article', (req, res) => {
var id = req.query.id
db.collection('articles').findOne({
"_id.$oid": id
}, function(err, article) {
res.send(article);
console.log(article)
});
});
บทความกำลังส่งคืน NULL
db.collection('articles').find( {"_id.$oid": id })
docs.mongodb.com/v3.2/core /document/#embedded-documents - person ajai Jothi   schedule 14.03.2017ObjectId(req.query.id)
- person Vince Bowdren   schedule 14.03.2017/api/article?id="yourid"
เพื่อค้นหา - person NERDYLIZARD   schedule 14.03.2017