- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用聚合 $lookup
在 MongoDB 中加入两个以上的集合。可以加入吗?给我一些例子。
这里我有三个集合:
用户
:
{
"_id" : ObjectId("5684f3c454b1fd6926c324fd"),
"email" : "admin@gmail.com",
"userId" : "AD",
"userName" : "admin"
}
用户信息
:
{
"_id" : ObjectId("56d82612b63f1c31cf906003"),
"userId" : "AD",
"phone" : "0000000000"
}
用户角色
:
{
"_id" : ObjectId("56d82612b63f1c31cf906003"),
"userId" : "AD",
"role" : "admin"
}
最佳答案
Mongodb 3.2及以上版本支持join功能。您可以通过聚合查询来使用联接。
您可以使用下面的示例来完成:
db.users.aggregate([
// Join with user_info table
{
$lookup:{
from: "userinfo", // other table name
localField: "userId", // name of users table field
foreignField: "userId", // name of userinfo table field
as: "user_info" // alias for userinfo table
}
},
{ $unwind:"$user_info" }, // $unwind used for getting data in object or for one record only
// Join with user_role table
{
$lookup:{
from: "userrole",
localField: "userId",
foreignField: "userId",
as: "user_role"
}
},
{ $unwind:"$user_role" },
// define some conditions here
{
$match:{
$and:[{"userName" : "admin"}]
}
},
// define which fields are you want to fetch
{
$project:{
_id : 1,
email : 1,
userName : 1,
userPhone : "$user_info.phone",
role : "$user_role.role",
}
}
]);
这将给出如下结果:
{
"_id" : ObjectId("5684f3c454b1fd6926c324fd"),
"email" : "admin@gmail.com",
"userName" : "admin",
"userPhone" : "0000000000",
"role" : "admin"
}
希望这会对您或其他人有所帮助。
谢谢
关于mongodb - 如何在 mongodb 中使用 $lookup 连接多个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35813854/
如果查找返回 null 该怎么办?我正在使用 org.openide.util.Lookup 的 Lookup.getDefault().lookup() ,它用于查找对象的实例。一般模式是传递一个
我尝试了两种方法来获取给定函数的 MethodHandle。 Method 1 Method m = MyClass.class.getMethod("myMethod", String.class,
前言 前面几节都是讲的基础内容,本节我们讲讲索引性能优化,当对大数据进行处理时首先想到的就是索引,一旦遇到这样的问题则手忙脚乱,各种查资料,为何平常不扎实基本功呢,我们由浅入深,简短的内容,深入的
我试图理解 System.Linq.Lookup类(class)。调用GetType()时,显示如下: System.Linq.Lookup`2+Grouping[System.Object,Syst
什么是依赖于参数的查找有哪些好的解释?许多人也称其为Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么不好? 如何运作? 最佳答案 Koenig查找或 Argument Dep
关于什么是参数相关查找有什么好的解释?许多人也称它为 Koenig Lookup。 最好我想知道: 为什么是好事? 为什么是坏事? 它是如何工作的? 最佳答案 Koenig 查找 , 或 Argume
我有两个 mongo 集合,一个包含约会提醒,另一个包含通知。我正在尝试返回给定的branchId/clinic_id中的所有存档:错误提醒的结果,并包括其已确认的:错误通知。另外,我想确保约会显示在
关于什么是参数依赖查找有什么好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查询 , 或
我正在 AX 2009 中的一个表单上创建一个项目。有一个名为“SubsPersonName”的查找字段,它使用以下方法对 ContactPerson 表执行查找。 public void looku
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何运作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何运作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何运作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有什么好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查找,或 A
以下函数将电话号码作为输入参数(例如 +436641234567 或 +436641234567)并在联系人数据库中执行两次查找:首先,识别属于该号码的用户(这已经有效),然后使用该 ID用户的 获取
我设置了一个运行centos6.2 64位的virtualbox客户端机器,主机是windows7 64位,并在客户端机器上设置了两个网卡,一个是NAT模式,一个是host-only模式,然后我就陷入
我是一名优秀的程序员,十分优秀!