mongodb中如何加入查询?
我有这样的用户文档集合:
User {
id:"001"
name:"John",
age:30,
friends:["userId1","userId2","userId3"....]
}
一个用户有很多朋友,我在 SQL 中有以下查询:
select * from user where in (select friends from user where id=?) order by age
我想在 MongoDB 中有类似的东西。
I have user document collection like this:
User {
id:"001"
name:"John",
age:30,
friends:["userId1","userId2","userId3"....]
}
A user has many friends, I have the following query in SQL:
select * from user where in (select friends from user where id=?) order by age
I would like to have something similar in MongoDB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
要使用聚合框架的 $lookup 功能通过一次查询获得所有内容,请尝试以下操作:
假设您的 User 集合的内容如下:
查询的结果将是:
To have everything with just one query using the $lookup feature of the aggregation framework, try this :
Let's say the content of your User collection is the following:
The result of the query will be:
编辑:此答案仅适用于 v3.2 之前的 MongoDb 版本。
您无法仅在一个查询中完成您想要的操作。您必须首先检索好友用户 id 列表,然后将这些 id 传递给第二个查询以检索文档并按年龄对它们进行排序。
Edit: this answer only applies to versions of MongoDb prior to v3.2.
You can't do what you want in just one query. You would have to first retrieve the list of friend user ids, then pass those ids to the second query to retrieve the documents and sort them by age.
https://docs.mongodb.org/manual/reference/operator/aggregation/lookup/
这是 mongodb 中连接查询的文档,这是 3.2 版本的新功能。
所以这会很有帮助。
https://docs.mongodb.org/manual/reference/operator/aggregation/lookup/
This is the doc for join query in mongodb , this is new feature from version 3.2.
So this will be helpful.
您可以在 Moongoose JS
.populate()
和{ populate : { path : 'field' } }
中使用。示例:
模特:
如果您想查看朋友的帖子,可以使用此。
如果你想查看朋友的帖子并带上所有评论,你也可以使用这个,如果找不到并且查询错误,你可以识别集合。
最后,如果您只想获取某个集合的某些字段,您可以使用属性 select 示例:
You can use in Moongoose JS
.populate()
and{ populate : { path : 'field' } }
.Example:
Models:
If you want to see your friends' posts, you can use this.
If you want to see your friends' posts and also bring all the comments, you can use this and too, you can indentify the collection if this not find and the query is wrong.
And to finish, if you want get only some fields of some Collection, you can use the propiertie select Example:
MongoDB 没有连接,但在你的情况下你可以这样做:
MongoDB doesn't have joins, but in your case you can do:
mongoDB 中的一种连接查询,是在一个集合中询问匹配的 id,将 id 放入列表(idlist)中,然后使用 $in : idlist 在其他(或相同)集合上查找使用
one kind of join a query in mongoDB, is ask at one collection for id that match , put ids in a list (idlist) , and do find using on other (or same) collection with $in : idlist
仅填充数组好友。
结果与此相同:
相同: Mongoose - 在数组上使用 Populate ObjectId
Only populate array friends.
Result is same like this:
Same this: Mongoose - using Populate on an array of ObjectId
您可以使用 playOrm 在一个查询中执行您想要的操作(使用 S-SQL 可扩展 SQL)。
You can use playOrm to do what you want in one Query(with S-SQL Scalable SQL).
它对我有用。
it works for me.
您可以使用
mongo-join-query
一次性完成此操作。其外观如下:结果将让您的用户按年龄排序并嵌入所有好友对象。
它是如何运作的?
在幕后
mongo-join-query
将使用您的 Mongoose 架构来确定要加入的模型,并创建一个 将执行联接和查询的聚合管道。You can do it in one go using
mongo-join-query
. Here is how it would look like:The result will have your users ordered by age and all of the friends objects embedded.
How does it work?
Behind the scenes
mongo-join-query
will use your Mongoose schema to determine which models to join and will create an aggregation pipeline that will perform the join and the query.