Javascript 如何在Mongoose中应用like查询?

Javascript 如何在Mongoose中应用like查询?,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我在均值堆栈中使用Mongoose,在应用Like查询时面临问题。我已经尝试了许多可以通过互联网帮助的选项,但都不管用 以下是我的模式的模型: var mongoose = require('mongoose'); var ItemTransactionSchema = new mongoose.Schema({ tid : Number, sku_id: Number, num_iid: Number, sub_order_tax_fee: String,

我在均值堆栈中使用Mongoose,在应用Like查询时面临问题。我已经尝试了许多可以通过互联网帮助的选项,但都不管用

以下是我的模式的模型:

var mongoose = require('mongoose');

var ItemTransactionSchema = new mongoose.Schema({
    tid : Number,
    sku_id: Number,
    num_iid: Number,
    sub_order_tax_fee: String,
    status:String,
    price:Number,
    invoice_no:String,
    payment:Number,
    cid:String,
    adjust_fee:Number,
    store_code:String,
    discount_fee:Number,
    total_fee:Number,
    barcode:String,
    itemdump:String,
    end_time:Date,
    storename:String,
    logistics_company:String
});

var item_transaction = mongoose.model('item_transaction', ItemTransactionSchema, 'item_transaction');
以下是我用于应用查询的代码:

var re = new RegExp(searched_key_word, 'i');

item_transaction.find({ "tid": { $regex: re }}, function (err, value) {
if (err) {
   callback(err, null);
}
else {
   callback(null, value);
}
}).select('-_id').limit(parseInt(limit));
请注意,如果我没有在
find()

以下是item_事务集合的视图:

> db.item_transaction.find()
{ "_id" : ObjectId("589dc1a3a46d1e96709db2c2"), "tid" : NumberLong("951107967214387"), "sku_id" : NumberLong("85864385869"), "num_iid" : NumberLong("45316210625"), "sub_order_tax_fee" :
 0, "status" : "TRADE_FINISHED", "price" : 195, "invoice_no" : "JL100296682", "payment" : 195, "cid" : 50011975, "adjust_fee" : 0, "store_code" : "", "discount_fee" : 0, "total_fee" : 1
95, "barcode" : NumberLong("670983077667"), "itemdump" : "{\"consign_time\": \"2015-05-27 09:52:14\", \"sku_id\": \"85864385869\", \"refund_status\": \"NO_REFUND\", \"seller_type\": \"B
\", \"sub_order_tax_fee\": \"0.00\", \"num\": 1, \"outer_sku_id\": \"670983077667\", \"shipping_type\": \"express\", \"is_daixiao\": false, \"logistics_company\": \"WnDirect\", \"sku_pr
operties_name\": \"u989cu8272u5206u7c7b:u7c89u7ea2u8272;u9ad8u5ea6:31CM\", \"title\": \"u82f1u56fdu8fdbu53e3jellycatu5bb3u7f9eu7cfbu5217u7c89u8272u90c1u91d1u9999u90a6u5c3cu5154u8d85u67d
4u8f6fu6bdbu7ed2u73a9u5177u516cu4ed4\", \"snapshot_url\": \"h:951107967214387_1\", \"sub_order_tax_rate\": \"0\", \"order_from\": \"TAOBAO\", \"status\": \"TRADE_FINISHED\", \"price\":
\"195.00\", \"oid\": 951107967214387, \"buyer_rate\": false, \"invoice_no\": \"JL100296682\", \"payment\": \"195.00\", \"seller_rate\": false, \"cid\": 50011975, \"discount_fee\": \"0.0
0\", \"adjust_fee\": \"0.00\", \"num_iid\": 45316210625, \"is_oversold\": false, \"end_time\": \"2015-06-16 09:52:34\", \"total_fee\": \"195.00\", \"pic_path\": \"http://img04.taobaocdn
.com/bao/uploaded/i4/TB1yryuHVXXXXaRXVXXXXXXXXXX_!!0-item_pic.jpg\"}", "end_time" : "2015-06-16 09:52:34", "storename" : "TMALL", "logistics_company" : "WnDirect" }
{ "_id" : ObjectId("589dc1a3a46d1e96709db2c3"), "tid" : NumberLong("967597019777082"), "sku_id" : NumberLong("91148011839"), "num_iid" : NumberLong("45361707245"), "sub_order_tax_fee" :
 0, "status" : "TRADE_FINISHED", "price" : 195, "invoice_no" : "JL100306378", "payment" : 195, "cid" : 50011975, "adjust_fee" : 0, "store_code" : "", "discount_fee" : 0, "total_fee" : 1
95, "barcode" : NumberLong("670983073997"), "itemdump" : "{\"consign_time\": \"2015-06-24 09:57:50\", \"sku_id\": \"91148011839\", \"refund_status\": \"NO_REFUND\", \"seller_type\": \"B
\", \"sub_order_tax_fee\": \"0.00\", \"num\": 1, \"outer_sku_id\": \"670983073997\", \"shipping_type\": \"express\", \"is_daixiao\": false, \"logistics_company\": \"WnDirect\", \"sku_pr
operties_name\": \"u989cu8272u5206u7c7b:u94f6u8272;u9ad8u5ea6:u4e2du53f7 - u9ad831cm\", \"title\": \"u82f1u56fdu8fdbu53e3jellycatu7ecfu5178u5bb3u7f9eu7cfbu5217u5bb3u7f9eu90a6u5c3cu5154u
94f6u8272u8d85u67d4u8f6fu6bdbu7ed2u73a9u5177u516cu4ed4\", \"snapshot_url\": \"h:967597019777082_1\", \"sub_order_tax_rate\": \"0\", \"order_from\": \"WAP,WAP\", \"status\": \"TRADE_FINI
SHED\", \"price\": \"195.00\", \"oid\": 967597019777082, \"buyer_rate\": false, \"invoice_no\": \"JL100306378\", \"payment\": \"195.00\", \"seller_rate\": false, \"cid\": 50011975, \"di
scount_fee\": \"0.00\", \"adjust_fee\": \"0.00\", \"num_iid\": 45361707245, \"is_oversold\": false, \"end_time\": \"2015-07-10 09:17:15\", \"total_fee\": \"195.00\", \"pic_path\": \"htt
ps://img.alicdn.com/bao/uploaded/i3/TB1nPmtHVXXXXXfaXXXXXXXXXXX_!!0-item_pic.jpg\"}", "end_time" : "2015-07-10 09:17:15", "storename" : "TMALL", "logistics_company" : "WnDirect" }
{ "_id" : ObjectId("589dc1a3a46d1e96709db2c4"), "tid" : NumberLong("1075205771411026"), "sku_id" : NumberLong("85662450758"), "num_iid" : NumberLong("45315502492"), "sub_order_tax_fee"
: 0, "status" : "TRADE_FINISHED", "price" : 100, "invoice_no" : "JL100301165", "payment" : 100, "cid" : 50011975, "adjust_fee" : 0, "store_code" : "", "discount_fee" : 0, "total_fee" :
100, "barcode" : NumberLong("670983062595"), "itemdump" : "{\"consign_time\": \"2015-06-04 10:15:09\", \"sku_id\": \"85662450758\", \"refund_status\": \"NO_REFUND\", \"seller_type\": \"
B\", \"sub_order_tax_fee\": \"0.00\", \"num\": 1, \"outer_sku_id\": \"670983062595\", \"shipping_type\": \"express\", \"is_daixiao\": false, \"logistics_company\": \"WnDirect\", \"sku_p
roperties_name\": \"u989cu8272u5206u7c7b:u7c73u8272;u9ad8u5ea6:13cm\", \"title\": \"u82f1u56fdu8fdbu53e3jellycatu7ecfu5178u5bb3u7f9eu7cfbu5217 u7ecfu5178u6b3eu90a6u5c3cu5154u7c73u8272u8

公平地说,您只能在字符串中运行正则表达式。因此,这里有两个选项:

选项1-将tid字段转换为字符串,然后可以运行以下命令:

db.item_transaction.find({tid: { $regex : /"yourRegex"/ }})
你唯一的缺点就是把TID转换成字符串(我想这不会给你带来太多麻烦,因为你还在建模)

选项2-使用$gt和$lt进行搜索。如果您想要的是一个真正的数字,您可以使用$gt(大于)和$lt(小于)运算符获取范围内的数字。我不建议这样做,因为它不是正则表达式,但我只是为了了解情况而告诉您。您可以这样使用它:

db.item_transaction.find({ tid: {$gt : 100} })

希望我的回答是有帮助的

正如我所担心的那样,您只能在字符串中运行正则表达式。因此,您有两个选择:

选项1-将tid字段转换为字符串,然后可以运行以下命令:

db.item_transaction.find({tid: { $regex : /"yourRegex"/ }})
你唯一的缺点就是把TID转换成字符串(我想这不会给你带来太多麻烦,因为你还在建模)

选项2-使用$gt和$lt进行搜索。如果您想要的是一个真正的数字,您可以使用$gt(大于)和$lt(小于)运算符获取范围内的数字。我不建议这样做,因为它不是正则表达式,但我只是为了了解情况而告诉您。您可以这样使用它:

db.item_transaction.find({ tid: {$gt : 100} })

希望我的回答有帮助

我这样使用它在运行时将数字“Path/column”转换为字符串

item_transaction.find({ "$where": "function() { return this.tid.toString().match(/"+searched_key_word+"/) != null; }" }, function (err, value) {
    if (err) {
        callback(err, null);
    }
    else {
        callback(null, value);
    }
}).select('-_id').limit(parseInt(limit)); 

我这样使用它在运行时将数字“Path/column”转换为字符串

item_transaction.find({ "$where": "function() { return this.tid.toString().match(/"+searched_key_word+"/) != null; }" }, function (err, value) {
    if (err) {
        callback(err, null);
    }
    else {
        callback(null, value);
    }
}).select('-_id').limit(parseInt(limit)); 

可能重复@Naeem Shaikh我在@Vishnu:You reference link上尝试了解决方案。在我的数据库中,值是数字,我在运行时将其转换为字符串。可能重复@Naeem Shaikh我在@Vishnu:You reference link上尝试了解决方案。在我的数据库中,值是数字,我将其转换为st运行时响铃。