什么Javascript库可以针对对象评估类似MongoDB的查询谓词? [英] What Javascript library can evaluate MongoDB-like query predicates against an object?

查看:133
本文介绍了什么Javascript库可以针对对象评估类似MongoDB的查询谓词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个javascript库允许我在类似于MongoDB查询语言的DSL中表达对象谓词?为了清楚大型程序,我希望能够说:

Is there a javascript library that will allow me to express object predicates in a DSL similar to MongoDB's query language? For the sake of clarity in a large program, I'd like to be able to say:

var obj = { 
    a: 1, 
    b: 'abcdefg' 
}, qry = { 
    a: { $gt: 0 }, 
    b: /^abc/ 
}; 

if(query(qry).matches(obj)) { 
    // do something appropriate since 
} 

而不是:

var obj = { 
    a: 1, 
    b: 'abcdefg' 
}; 
if(obj.a>0 && qry.b.test(obj.b)) { 
    // do something appropriate 
} 

我正在使用Node.js,所以 NPM 会很棒。如果库可以从数组中选择对象以及仅匹配单个对象,那将是一个额外的好处。

I'm using Node.js, so anything on NPM would be great. It would be an added bonus if the library can select objects out of an array as well as just matching individual objects.

我回顾了这两个相关问题,但它们对我的情况并不是特别有用:

I reviewed these two related questions, but they weren't particularly helpful for my situation:

  • Implementing goMongoDB-like Query expression object evaluation
  • Evaluating MongoDB-like JSON Queries in PHP

推荐答案

好的我找到了答案: Sift.js

现在答案很长:这是之前询问并回答。重点是:

Now for the long answer: This has been asked and answered before. The salient points are:


  • 如果你真的想要Mongo语法,请使用Sift

  • 如果你想要更主流,像其他人一样使用 Underscore.js 。除了基本上用sift略有不同的sift做的事实之外,它还有很多方便的功能。

  • 您可能根本不需要任何库 - 现代浏览器支持许多有用的功能直接在数组原型上,如 filter()例如。

  • Use Sift if you really want Mongo syntax
  • If you want to be more mainstream, use Underscore.js like everyone else. It has heaps of handy functions in addition to the fact that it basically does what sift does with a slightly different syntax.
  • You may not need any library at all - modern browsers support many useful functions directly on the Array prototype, like filter() for example.

作为最后一点, mongodb-riff 似乎试图做类似的事情,但目前该页面明确指出它不起作用 - 也许它被抛弃了。但他的自述文件至少有价值:-),他提到了筛选和查询引擎更成熟,虽然对我来说太复杂了!

As a final note, mongodb-riff appears to be trying to do something similar but currently the page states clearly that it doesn't work - perhaps it's abandoned. But his readme is at least of value :-), he mentions sift and Query Engine which looks more mature, though too complicated for me!

我个人会选择Underscore,因为现在我第一次看到它,我意识到它有大量我需要的方便的东西,而且我真的只想做简单的功能,比如Underscore中的 _。find()。但我想如果你想做更复杂的类似mongo的查询,你可以用Sift用更少的LOC来做。

Personally I'm going to go with Underscore because now that I've looked into it for the first time, I realise that it has heaps of handy stuff I need, plus I really only wanted to do simple functions like what would be _.find() in Underscore. But I guess if you want to do more complicated mongo-like queries, you'll do it in less LOC with Sift.

这篇关于什么Javascript库可以针对对象评估类似MongoDB的查询谓词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆