如何解决MongoDB:如何使用查找查询填充嵌套对象?
我正在获取对其他集合具有一些嵌套引用的记录列表,我想使用 mongoDb 聚合查找查询填充对象数组内的嵌套 ObjectId。
DB 集合结构是这样的:
{
subject: {type: String},body: {type: String},recipients: [{
userId: {type: mongoose.Schema.Types.ObjectId,ref: 'User'},stutus: {type: String,enum: ['pending','accepted','rejected'],default:'pending'}
}],sender: {type: mongoose.Schema.Types.ObjectId,ref: 'User'}
}
我的期望:
[{
subject: 'Some subject here.',body: 'Lorem ipsum dolor emit set',recipients: [{
userId: {firstName: 'John',lastName: 'Doe'},status: 'accepted'
},{
userId: {firstName: 'Jane',status: 'accepted'
}],sender: {firstName: 'Jacobs','lastName': 'Doe'}
},{
subject: 'Some subject here.',recipients: [{
userId: {firstName: 'Jane',status: 'rejected'
},{
userId: {firstName: 'John','lastName': 'Doe'}
}]
任何帮助将不胜感激。
解决方法
-
$unwind
解构recipients
数组 -
$lookup
包含recipients.userId
的用户集合 -
$unwind
解构recipients.userId
数组 -
$lookup
包含sender
的用户集合 -
$unwind
解构sender
数组 -
$group
by_id
并重建recipients
数组
db.mails.aggregate([
{ $unwind: "$recipients" },{
$lookup: {
from: "users",localField: "recipients.userId",foreignField: "_id",as: "recipients.userId"
}
},{ $unwind: "$recipients.userId" },localField: "sender",as: "sender"
}
},{ $unwind: "$sender" },{
$group: {
_id: "$_id",recipients: { $push: "$recipients" },subject: { $first: "$subject" },body: { $first: "$body" },sender: { $first: "$sender" }
}
}
])
,
试试这个:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" package="com.arge24.assistant24_mobile">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" tools:ignore="ProtectedPermissions" />
<application android:name="io.flutter.app.FlutterApplication" android:label="Assistant 24" android:allowBackup="false" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:usesCleartextTraffic="true">
<activity android:name=".MainActivity" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize" android:showWhenLocked="true" android:turnScreenOn="true">
<meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" />
<meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable" android:resource="@drawable/launch_background" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="BootCompleteReceiver" android:enabled="true" android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT" />
</intent-filter>
</receiver>
</application>
</manifest>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。