我对同一集合有多个查询:
第一个查询:
db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0})
第一个查询结果
{
"currentStep" : [
"flowkuec8ta1o"
]
}
我将第二个查询的第一个查询结果'flowkuec8ta1o'用于查询(查询1结果中的'currentStep'用作以下第二个查询中键'previousStep'的值):< / p>
第二个查询:
db.getCollection('messagelogs').find({'previousStep':'flow1pemwl7ws'},{'userMessage':1})
第二个查询结果:
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
如何将两个查询合并为一个?
请不要在this问题中标记该问题。我尝试了该问题的解决方案,但并没有为我提供理想的结果。有人可以帮我吗?
附加示例文档1(从中获取“ currentStep”的位置):
{
"_id" : ObjectId("5cec2dc69b806c4a00f91f16"),
"currentStep" : [
"flowkuec8ta1o"
],
"previousStep" : [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage" : "Liked It :)",
"intents" : [
{
"_id" : ObjectId("5cec2dc69b806c4a00f91f17"),
"intent" : "General_Positive_Feedback",
"score" : "0.9774518966674806"
}
]
}
附加示例文档2(将从查询1中获取的“ currentStep”值用作第二个查询中的“ previousStep”)
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"currentStep" : [],
"previousStep" : [
"flowkuec8ta1o"
],
"userMessage" : "ILikedIt#1",
"intents" : [
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f19"),
"intent" : "Feedback",
"score" : "1"
}
]
}
示例输出:
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
根据@Fanparks请求添加示例文档:
以下两个文档与第一个查询匹配(以获取“ currentStep”)
文档1:
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Liked It :)",
"intents": [{
"_id": ObjectId("5cec2dc69b806c4a00f91f17"),
"intent": "General_Positive_Feedback",
"score": "0.9774518966674806"
}]
}
文档2:
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Just Okay, Could Be better",
"intents": [{
"_id": ObjectId("5cec2f1a9b806c4a00f91f41"),
"intent": "General_Positive_Feedback",
"score": "1"
}]
}
以下文档与第二个查询匹配(“ previousStep”与从第一个查询获取的“ currentStep”匹配)
文档1:
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "ILikedIt#1",
"intents": [{
"_id": ObjectId("5cec2dd29b806c4a00f91f19"),
"intent": "Feedback",
"score": "1"
}]
}
文档2:
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "JustOkayCouldBeBetter#1",
"intents": [{
"_id": ObjectId("5cec2f2b9b806c4a00f91f43"),
"intent": "Feedback",
"score": "1"
}]
}
文档3:
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "I'mGivingAPositiveFeedback",
"intents": [{
"_id": ObjectId("5ced84e2fdf046078c85d9cb"),
"intent": "Feedback",
"score": "1"
}]
}
文档4:
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "I'm giving a positive feedback for the second time!",
"intents": [{
"_id": ObjectId("5cede37dfdf046078c85d9e0"),
"intent": "Feedback",
"score": "1"
}]
}
所需的输出:
/* 1 */
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
/* 2 */
{
"_id" : ObjectId("5cec2f2b9b806c4a00f91f42"),
"userMessage" : "JustOkayCouldBeBetter#1"
}
/* 3 */
{
"_id" : ObjectId("5ced84e2fdf046078c85d9ca"),
"userMessage" : "I'mGivingAPositiveFeedback"
}
/* 4 */
{
"_id" : ObjectId("5cede37dfdf046078c85d9df"),
"userMessage" : "I'm giving a positive feedback for the second time!"
}
**实际输出:**
/* 1 */
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
/* 2 */
{
"_id" : ObjectId("5cec2f2b9b806c4a00f91f42"),
"userMessage" : "JustOkayCouldBeBetter#1"
}
/* 3 */
{
"_id" : ObjectId("5ced84e2fdf046078c85d9ca"),
"userMessage" : "I'mGivingAPositiveFeedback"
}
/* 4 */
{
"_id" : ObjectId("5cede37dfdf046078c85d9df"),
"userMessage" : "I'm giving a positive feedback for the second time!"
}
/* 5 */
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
/* 6 */
{
"_id" : ObjectId("5cec2f2b9b806c4a00f91f42"),
"userMessage" : "JustOkayCouldBeBetter#1"
}
/* 7 */
{
"_id" : ObjectId("5ced84e2fdf046078c85d9ca"),
"userMessage" : "I'mGivingAPositiveFeedback"
}
/* 8 */
{
"_id" : ObjectId("5cede37dfdf046078c85d9df"),
"userMessage" : "I'm giving a positive feedback for the second time!"
}
答案 0 :(得分:1)
您可以使用以下聚合
db.getCollection('messagelogs').aggregate([
{ "$match": { "intents.intent": "General_Positive_Feedback" }},
{ "$lookup": {
"from": "messagelogs",
"let": { "currentStep": "$currentStep" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$previousStep", "$$currentStep"] }}},
{ "$project": { "userMessage": 1 }}
],
"as": "previousStep"
}},
{ "$unwind": "$previousStep" },
{ "$replaceRoot": { "newRoot": "$previousStep" }},
{ "$group": {
"_id": "$userMessage",
"id": { "$first": "$_id" }
}},
{ "$project": {
"_id": "$id",
"userMessage": "$_id"
}}
])