TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

MongoDB Is Abusing JSON

43 点作者 sm_sohan超过 12 年前

15 条评论

mglukhovsky超过 12 年前
Here's what this query looks like in RethinkDB (also based on JSON documents):<p><pre><code> r.table('orders') .pluck('cust_id','ord_date','price') .groupBy('cust_id','ord_date', r.sum('price')). .filter(r.row('reduction').gt(250)) </code></pre> We use the hard-coded attribute 'reduction' because <i>groupBy</i> automatically gets compiled to our distributed map-reduce infrastructure. There is currently no <i>as</i> command (though it could easily be simulated with <i>map</i>). I'll add a GitHub issue for this shortly, since we should add sugar for it.
评论 #5076292 未加载
评论 #5076339 未加载
评论 #5076291 未加载
cheald超过 12 年前
Aggregation is absolutely one of Mongo's weaknesses. It's not great at ad-hoc aggregation like MySQL or whatnot is, and the fact that it tends to lend itself to denormalized data makes SQL-style reporting clunky at best.<p>It does a lot of things better than SQL, too. Consider, for example, the query "Give me a list of all posts with all of these tags, by any of these authors, sorted by post date descending"<p><pre><code> db.posts.find({ tags: {$all: ["foo", "bar", "baz"]}, author: {$in: ["Joe", "Jane"]} }).sort({post_date: -1}) </code></pre> In SQL, you'd end up with something like:<p><pre><code> SELECT posts.* FROM posts INNER JOIN post_tags t1 ON t1.tag = "foo" AND t1.post_id = posts.id INNER JOIN post_tags t2 ON t2.tag = "bar" AND t2.post_id = posts.id INNER JOIN post_tags t3 ON t3.tag = "baz" AND t3.post_id = posts.id WHERE posts.author = "Joe" or post.author = "Jane" ORDER BY post_date DESC; </code></pre> Its strength is denormalization; since you can denormalize entire lists or maps of data into a document, and then index and query on them, you can end up performing queries that would be ridiculously ugly and tedious in SQL.
评论 #5076367 未加载
评论 #5076414 未加载
mattparlane超过 12 年前
I've been through the hassle of programatically piecing together complex SQL queries, and I'd far rather be able to just put together hashes that represent my query.<p>SQL was originally designed so that people who were savvy but not necessarily developers were able to query databases, but I can't think of the last time my boss would have wanted to run some random query against our production database.
评论 #5076245 未加载
评论 #5077467 未加载
评论 #5076361 未加载
byoung2超过 12 年前
There is always <a href="http://querymongo.com/" rel="nofollow">http://querymongo.com/</a> which will convert SQL to a MongoDB query.
评论 #5076215 未加载
评论 #5076129 未加载
aidos超过 12 年前
I'm just finishing off a project that was built using Mongo and I've run into this as well.<p>Other gotchas too, like feeling like you can store any old json structure in your db when you can't.<p>Dots are reserved because they're part of the query syntax. Fair enough, but it's pretty crappy to have to unpick a whole data structure because it was fine until a random bit of UGC was entered (that's where my last fews hours just went).<p>It does feel like the data and the query syntax are too crossed over to me.
benologist超过 12 年前
You picked an ugly mongo query, and there are many. You compared it to a concise SQL query, and there are many that are not.<p>MongoDB's limit(x) and skip(y) are a shitload nicer than most of Microsoft's ideas about pagination. It was only in SQL Server 2012 that they came up with "OFFSET" instead of "google it".... <a href="http://stackoverflow.com/questions/2244322/how-to-do-pagination-in-sql-server-2008" rel="nofollow">http://stackoverflow.com/questions/2244322/how-to-do-paginat...</a>
评论 #5076180 未加载
评论 #5076335 未加载
samarudge超过 12 年前
JSON is used as a query language because it's fast, easy to parse and easy to generate dynamically. If you have a query interface for users, SQL is probably a better choice, but Mongo chose JSON for performance reasons.<p>If you don't like dealing with it directly, use something like MongoEngine so you're not working with the raw queries, or if having readable, easy to understand queries is important, use a SQL database.<p>Everything is a compromise, with Mongo's query language you're sacrificing readability for performance.<p>( This is not a comparison of a SQL database to Mongo, just the time it takes for a SQL engine to parse the query into an execution plan )
评论 #5076174 未加载
评论 #5076393 未加载
taylorbuley超过 12 年前
I don't think it's fair to take JSON out of context from the rest of the query API. Or maybe it's being overly generous, I'm not sure. Either way, this is the same way object literals are constructed in JavaScript. So is the beef w/JavaScript?<p>For me the Mongo shell is just enough so-called "richness" and "expressiveness" (Try it yourself: <a href="http://try.mongodb.org/" rel="nofollow">http://try.mongodb.org/</a>). There's a certain magic to passing objects to functions (and being able to, say, read the body of a function by typing that object into the CLI).
pestaa超过 12 年前
TL;DR: JSON sucks for representing queries.
评论 #5076131 未加载
maxharris超过 12 年前
Meh. I count roughly the same number of tokens either way. I really don't think this is that big of a deal.
gumbo超过 12 年前
I feel the same about the query language of Mongo. The first goal of a query language should be ease of use. With mongo having to type all those extra characters quotes, brakets, square brakets, colons is very annoying. You need to type a lot to get any reasonable output.
评论 #5076441 未加载
andrewmunsell超过 12 年前
MongoDB is a NoSQL-type database, so it wouldn't make sense to have a SQL query interface... I think they did a good job with the API for not using SQL.<p>Plus, the API isn't really <i>abusing</i> JSON. It isn't pretty, but it's not abuse.
评论 #5076237 未加载
deepinsand超过 12 年前
I think it's great. I've spent too much time parsing SQL strings into well typed data structures, and you get it for free with Mongo.
评论 #5077592 未加载
coenhyde超过 12 年前
Mongodb queries are much easier to dynamically build than SQL. And this is so because Mongodb uses JSON for queries.
gummydude超过 12 年前
have you seen Elasticsearch API?