[英]MongoDB Check if a document/record exists
Im trying to use .find({}) with mongogb and unfortunately its not giving me the response I was expecting, I'm unsure how to check if the document exists or not? 我试图将.find({})与mongogb结合使用,但不幸的是,它没有给我期望的响应,我不确定如何检查文档是否存在? What I'm trying to do is say:
我想做的是说:
If a document does exist then do something.. IE send a response back but If a document doesn't exist then create the document, 如果文档确实存在,则执行某项操作。.IE发送回响应,但如果文档不存在,则创建该文档,
unfortunately I know that a document doesnt exist yet it must be picking up the wrong thing with 'if (docs)' but then when I change it to something else then it always creates records!? 不幸的是,我知道一个文档不存在,但是它一定是用'if(docs)'来拾取错误的东西,但是当我将其更改为其他内容时,它总是会创建记录!
The code: 编码:
addRefund : (refundCalc, callback) => {
order_number = refundCalc.refundDetails.customer_details.order.order_number;
dbconnect.createConnection()
refund.find({order_number: order_number}, (err, docs) => {
if (docs) {
console.log('docss!!!!!!!!!!!!!' + JSON.stringify(docs));
console.log('calling within error!!!!!!')
let notStored = {"refundDocStored" : "False"}
callback(notStored)
dbconnect.closeConnection();
}
else {
refund.create(refundCalc).then((refunddoc) => {
let filestored = {"refundDocStored" : "True"}
dbconnect.closeConnection();
callback(filestored)
}).catch((err)=> {
console.log(err);
dbconnect.closeConnection();
})
}
})
},
the schema: 模式:
const refundSchema = new Schema({
domain : { type: String},
refundDetails : {
customer_details : [],
refund : {
shipping : {
amount : { type: Number},
tax : {type : Number},
maximum_refundable : {type : Number}
},
refund_line_items: [],
transactions: []
}
}
}); });
The orders are stored within the refundDetails like this: 订单存储在退款详细信息中,如下所示:
"refundDetails":{"customer_details":{"order":{"order_number":1021
It simply doesnt seem to work for me! 它似乎根本不适合我! if a document exists i cant seem to actually prove that it does?
如果文件存在,我似乎无法真正证明它确实存在?
Any help would be great, thanks! 任何帮助将是巨大的,谢谢!
You are using the wrong search query. 您使用了错误的搜索查询。 You are searching for order_number which is a property of an object inside another object.
您正在搜索order_number,它是另一个对象内部的一个对象的属性。 You have to reference the order_number full path in your query ie
{"refundCalc.refundDetails.customer_details.order.order_number" : order_number}
您必须在查询中引用order_number的完整路径,即
{"refundCalc.refundDetails.customer_details.order.order_number" : order_number}
addRefund : (refundCalc, callback) => {
order_number = refundCalc.refundDetails.customer_details.order.order_number;
dbconnect.createConnection()
refund.find({"refundCalc.refundDetails.customer_details.order.order_number": order_number}, (err, docs) => {
if (docs) {
console.log('docss!!!!!!!!!!!!!' + JSON.stringify(docs));
console.log('calling within error!!!!!!')
let notStored = {"refundDocStored" : "False"}
callback(notStored)
dbconnect.closeConnection();
}
else {
refund.create(refundCalc).then((refunddoc) => {
let filestored = {"refundDocStored" : "True"}
dbconnect.closeConnection();
callback(filestored)
}).catch((err)=> {
console.log(err);
dbconnect.closeConnection();
})
}
})
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.