gpt4 book ai didi

node.js - Mongoose 查询 : how to pass a variable

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:32 25 4
gpt4 key购买 nike

如何在下面的 Xcom.find({ $where : "this.NIC_No == '5'"} 中传递 'n' 而不是硬编码的 '5'

exports.query_by_nic = function ( req, res ){
var n = req.body.NIC_No;
console.log(n); //n prints correctly as expected
Xcom.find({ $where : "this.NIC_No == '5'" }, function ( err, xcoms ){ //works with '5' but how do I query for 'n'?
res.render( 'query_by_nic', {
title : 'Query by NIC',
xcoms : xcoms
});
console.log(err);
console.log(xcoms);
});
};

最佳答案

你可以这样做:

Xcom.find({ $where : "this.NIC_No == '" + variableThatContainsNumber + "'" }, function ( err, xcoms ){
...
});

或者更好:

Xcom.find({NIC_No: variableThatContainsNumber}, function(err, doc) {
...
});

第二个要好得多,因为它不需要在 MongoDB 中执行 JavaScript。

关于node.js - Mongoose 查询 : how to pass a variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16401030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com