【发布时间】:2016-09-03 06:23:59
【问题描述】:
我有一个在 mongo shell 中工作的查询
db.getCollection('insights').find({$and:[{author:/jim/i}]})
返回 2 条记录。
调用 waterline/sailsjs-mongo 的“或”代码
db('insights').find({
or: [
{or: [ {author: new RegExp( ".*"+"jim"+".*", 'i' )} ] }
]
}).limit(req.params.limit).skip(req.params.offset).exec(function (err, insights) { ... }
按预期返回 2 条记录。
如果我将“或”更改为“和”
db('insights').find({
or: [
{and: [ {author: new RegExp( ".*"+"jim"+".*", 'i' )} ] }
]
}).limit(req.params.limit).skip(req.params.offset).exec(function (err, insights) { ... }
我得到了 0 条记录,这是意料之外的。它应该返回 2 条记录。
我使用原生 mongodb javascript 客户端编写了一些代码。
var MongoClient = require('mongodb').MongoClient,
test = require('assert');
// Connection url
var url = 'mongodb://localhost:27017/infosystem';
// Connect using MongoClient
MongoClient.connect(url, function(err, db) {
// Create a collection we want to drop later
var col = db.collection('insights');
// Show that duplicate records got dropped
col.find({$and:[{author: new RegExp('.*Jim.*','i')}]}).toArray(function(err, items) {
test.equal(null, err);
test.equal(2, items.length);
db.close();
});
});
运行时不抛出任何断言。
node test_insights.js
mongodb 是 3.2.9 版本,sails-mongodb 和 waterline 是版本“0.12.1”
在sails-mongodb 中调用mongodb $and 查询的正确方法是什么?
【问题讨论】:
标签: node.js mongodb express sails.js