【问题标题】:Mongodb-Query db using result from another queryMongodb-Query db 使用来自另一个查询的结果
【发布时间】:2019-10-13 08:08:39
【问题描述】:

我对同一个集合有多个查询:

第一次查询:

db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0})    

第一次查询结果

{
"currentStep" : [ 
    "flowkuec8ta1o"
]
}

我将第一个查询结果“flowkuec8ta1o”用于第二个查询(查询 1 结果中的“currentStep”用作以下第二个查询中键“previousStep”的值):

第二次查询:

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!"
}

【问题讨论】:

  • 请解释为什么要合并这两个查询?您的上述两个查询之间有什么关系?我看不到I'm using the first query result 'flowkuec8ta1o' for 2nd query。第二个在哪里用的?发布一些样本集合和你想要的输出。
  • @Fanpark 我已根据您的要求更新了问题。请看一下

标签: mongodb mongodb-query aggregation-framework


【解决方案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"
  }}
])

MongoPlayground

【讨论】:

  • 如果在as 表达式中找到多个previousStep,那么它可能会为您提供多条记录,因为我们在这里执行的下一步是$unwind
  • 更新了我的答案
  • 你能用示例文件和输出解释一下吗
  • Fanpark,你能在这里看看这个问题吗?:stackoverflow.com/questions/56335641/…我想知道在两个日期之间获取文件的正确方法。你能看看吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-21
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多