[英]Mongodb $lookup Not working with _id
嘗試使用此查詢,返回查找為空
db.getCollection('tests').aggregate([
{$match: {typet:'Req'}},
{$project: {incharge:1}},
{$lookup:{
from: "users",
localField: "incharge", //this is the _id user from tests
foreignField: "_id", //this is the _id from users
as: "user"
}}
])
返回 json
[
{
"_id": "57565d2e45bd27b012fc4db9",
"incharge": "549e0bb67371ecc804ad23ef",
"user": []
},
{
"_id": "57565d2045bd27b012fc4cbb",
"incharge": "549e0bb67371ecc804ad21ef",
"user": []
},
{
"_id": "57565d2245bd27b012fc4cc7",
"incharge": "549e0bb67371ecc804ad24ef",
"user": []
}
]
我嘗試使用這篇文章,但沒有發生任何事情MongoDB 將項目字符串聚合到 ObjectId並使用此MongoDB $lookup with _id 作為 PHP 中的 foreignField
更新
這是文件“用戶”
{
"_id" : ObjectId("549e0bb67371ecc804ad24ef"),
"displayname" : "Jhon S."
},
{
"_id" : ObjectId("549e0bb67371ecc804ad21ef"),
"displayname" : "George F."
},
{
"_id" : ObjectId("549e0bb67371ecc804ad23ef"),
"displayname" : "Franc D."
}
我終於找到了解決方案,是我的 Schema 在帶有 ObjectId 的貓鼬中存在問題
我改變這個
var Schema = new Schema({
name: { type: String, required: true},
incharge: { type: String, required: true},
});
有了這個
var Schema = new Schema({
name: { type: String, required: true},
incharge: { type: mongoose.Schema.ObjectId, required: true},
});
並且正在工作
首先,斷言incharge
字段的類型是mongoose.Schema.Types.ObjectId
。 如果你仍然得到一個空數組,那可能是因為你使用的是你在 NodeJS 中聲明的模式名稱,而不是 MongoDB 使用的集合名稱。
來自UserSchema
文件的示例:
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const UserSchema = new Schema({
name: {
type: String,
required: true
},
incharge: {
type: Schema.Types.ObjectId,
required: true
},
})
const User = mongoose.model('User', UserSchema)
module.exports = User
上面的模型由 mongoose 命名為User
,但 mongoDB 中相應的集合命名為users
。 結果$lookup
寫成:
$lookup:{
from: "users", // name of mongoDB collection, NOT mongoose model
localField: "incharge", // referenced users _id in the tests collection
foreignField: "_id", // _id from users
as: "user" // output array in returned object
}
https://mongoosejs.com/docs/models.html
https://mongoosejs.com/docs/schematypes.html
您的查找查詢是正確的。 但它試圖將字符串類型 (incharge) 與 ObjectId (_id) 進行比較。 將字符串轉換為 ObjectId,如下所示。 它對我有用。
db.getCollection('tests').aggregate([
{$match: {typet:'Req'}},
{$project: {
incharge:{
$toObjectId:"$incharge"
}
},
{$lookup:{
from: "users",
localField: "incharge", //this is the _id user from tests
foreignField: "_id", //this is the _id from users
as: "user"
}}
您只需要使用"_id.str"
完成工作。
db.getCollection('tests').aggregate([
{$match: {typet:'Req'}},
{$project: {incharge:1}},
{$lookup:{
from: "users",
localField: "incharge", //this is the _id user from tests
foreignField: "_id.str", //this is the _id from users
as: "user"
}}
])
對我來說很好用。
將string
與ObjectId
進行比較不會引發錯誤,而是在聚合輸出文檔中發送一個空數組。 因此,您需要確保已將string
對象 id 轉換為 mongodb 的ObjectId
:
db.getCollection('tests').aggregate([
{$match: {typet:'Req'}},
{$set: {incharge: {$toObjectId: "$incharge"} }}, // keep the whole document structure, but replace `incharge` into ObjectId
{$lookup:{
from: "users",
localField: "incharge", //this is the _id user from tests
foreignField: "_id", //this is the _id from users
as: "user"
}}
])
您的查找查詢是完美的,但問題是您將incharge作為字符串存儲到數據庫中,而 _id : ObjectId('theID') 是一個對象而不僅僅是字符串,您不能將string
(' ') 與object
({})。 因此,最好的方法是將incharge鍵存儲為對象( mongoose.Schema.ObjectId )而不是模式中的字符串。
嘗試在這樣的聚合函數中將字符串類型的 incharge 更改為 ObjectId
{
$project : {
incharge : {
$toObjectId : "$incharge"
}
}
}
如果您已將密鑰存儲在字符串中,則可以使用 $addFields (聚合)將密鑰轉換為 objectId
{
"$addFields": {
incharge: {
"$toObjectId": "$incharge"
}
}
}
試試這個:
{
from: 'categories',
let: { cid: { $toObjectId: '$category_id' } },
pipeline: [
{ $match: { $expr: { $eq: ['$_id', '$$cid'] } } },
],
as: 'category_data'
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.