我需要左反加入 mongodb 与查找和匹配聚合函数
I need left anti join in mongodb with lookup and match aggregate function
我想在 MongoDB
中转换此查询
Select * 来自不存在的问题(select * 来自 Solved_question 其中 questionid = id 和 username=username)
集合架构
question:{
_id:ObjectId,
title:String,
desc:string,
Author:string
},
User:{
_id:ObjectId,
Email:string,
Password:string
},
Solvedquestioncollection:{
Id:_id,
QuestionId:{ type:mongoose.Schema.ObjectId, ref:"Question" },
UserId:{ type:mongoose.Schema.ObjectId, ref:"User" }
}
示例文档
Question:[
{
_id:ObjectId('1'),
title:"main component of computer",
desc:"some desc for this"
author:"ashick"
},
{
_id:ObjectId('2'),
title:"Advantage of CPU",
desc:"some desc for this"
author:"ashick"
},
]
User:[
{
_id:ObjectId('1'),
email:"as@g.com",
password:"12345"
},
{
_id:ObjectId('2'),
email:"df@g.com",
password:"345"
}
]
solvedquestion:[
{
_id:ObjectId('1'),
question:ObjectId('2'),
userId:ObjectId('1')
},
{
_id:ObjectId('2'),
question:ObjectId('2'),
userId:ObjectId('2')
}
]
我想在问题集中获取特定用户没有解决的问题
我试试这个
question = await Question.aggregate([
{
$lookup: {
from: "solvedquestions",
let: { userId: "$userId" },
pipeline: [
{
$match: {
$expr: {
$eq: ["ObjectId('60ebc6b9980b8e1f8cffe34b'"), "$$userId"],
},
},
},
],
as: "resultingArray",
},
},
]);
但它的 Return 空数组
感谢您的回答
try {
let aggregate = Question.aggregate([
{
$lookup: {
from: 'solvedquestions',
let: {
question_id: '$_id',
question_userId: '$user'
},
pipeline: [ {
$match: {
$expr: {
$and: [
{
$eq: ['$QuestionId', '$$question_id']
},
{
$eq: ['$userId', '$$question_userId']
}
]
}
}
}],
as: 'results'
}
}
]);
return await aggregate.exec();
} catch (error) {
console.error('Error ->',error);
}
希望对您有所帮助,如有任何疑问,请告诉我。乐于助人:)
我想在 MongoDB
中转换此查询Select * 来自不存在的问题(select * 来自 Solved_question 其中 questionid = id 和 username=username)
集合架构
question:{
_id:ObjectId,
title:String,
desc:string,
Author:string
},
User:{
_id:ObjectId,
Email:string,
Password:string
},
Solvedquestioncollection:{
Id:_id,
QuestionId:{ type:mongoose.Schema.ObjectId, ref:"Question" },
UserId:{ type:mongoose.Schema.ObjectId, ref:"User" }
}
示例文档
Question:[
{
_id:ObjectId('1'),
title:"main component of computer",
desc:"some desc for this"
author:"ashick"
},
{
_id:ObjectId('2'),
title:"Advantage of CPU",
desc:"some desc for this"
author:"ashick"
},
]
User:[
{
_id:ObjectId('1'),
email:"as@g.com",
password:"12345"
},
{
_id:ObjectId('2'),
email:"df@g.com",
password:"345"
}
]
solvedquestion:[
{
_id:ObjectId('1'),
question:ObjectId('2'),
userId:ObjectId('1')
},
{
_id:ObjectId('2'),
question:ObjectId('2'),
userId:ObjectId('2')
}
]
我想在问题集中获取特定用户没有解决的问题
我试试这个
question = await Question.aggregate([
{
$lookup: {
from: "solvedquestions",
let: { userId: "$userId" },
pipeline: [
{
$match: {
$expr: {
$eq: ["ObjectId('60ebc6b9980b8e1f8cffe34b'"), "$$userId"],
},
},
},
],
as: "resultingArray",
},
},
]);
但它的 Return 空数组
感谢您的回答
try {
let aggregate = Question.aggregate([
{
$lookup: {
from: 'solvedquestions',
let: {
question_id: '$_id',
question_userId: '$user'
},
pipeline: [ {
$match: {
$expr: {
$and: [
{
$eq: ['$QuestionId', '$$question_id']
},
{
$eq: ['$userId', '$$question_userId']
}
]
}
}
}],
as: 'results'
}
}
]);
return await aggregate.exec();
} catch (error) {
console.error('Error ->',error);
}
希望对您有所帮助,如有任何疑问,请告诉我。乐于助人:)