在 MongoDB 中,您可以使用如下所示的 JSON 样式对象来查询集合:
db.things.find({ x : { $ne : 3 }, y : 'foo' });
我想重用那个 { x : { $ne : 3 }, y : 'foo' }
位并用它来过滤 JavaScript 对象数组。
是否有任何代码/库可以做到这一点,并且支持所有查询选项(或者尽可能多地有意义)?
最佳答案
好的,这是另一个尝试:
sift.js (npm: sift) by Craig Condon is a MongoDB-inspired array filtering library. It’s a bit like an alternative to Underscore for people who love MongoDB. Sift.js supports operators like $in and $gt, but can also filter arrays based on functions and even works with deeply-nested objects in arrays.
Craig has provided a few examples that should look familiar to Mongo users:
var sift = require('sift');
sift({ $in: ['hello','world'] }, ['hello','sifted','array!']); //
['hello']
来源(已编辑):Daily JS,但网站似乎已关闭。
关于javascript - 有没有办法使用 MongoDB 查询对象来过滤常规 JavaScript 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8695718/