Mongodb使用另一个查询的结果查询数据库
我对同一集合有多个查询: 第一次查询:Mongodb使用另一个查询的结果查询数据库,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我对同一集合有多个查询: 第一次查询: db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0}) { "currentStep" : [ "flowkuec8ta1o" ] } db.getCollection('messagelogs').find({'previousStep':'flow1pemwl7ws'
db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0})
{
"currentStep" : [
"flowkuec8ta1o"
]
}
db.getCollection('messagelogs').find({'previousStep':'flow1pemwl7ws'},{'userMessage':1})
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Liked It :)",
"intents": [{
"_id": ObjectId("5cec2dc69b806c4a00f91f17"),
"intent": "General_Positive_Feedback",
"score": "0.9774518966674806"
}]
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Just Okay, Could Be better",
"intents": [{
"_id": ObjectId("5cec2f1a9b806c4a00f91f41"),
"intent": "General_Positive_Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "ILikedIt#1",
"intents": [{
"_id": ObjectId("5cec2dd29b806c4a00f91f19"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "JustOkayCouldBeBetter#1",
"intents": [{
"_id": ObjectId("5cec2f2b9b806c4a00f91f43"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "I'mGivingAPositiveFeedback",
"intents": [{
"_id": ObjectId("5ced84e2fdf046078c85d9cb"),
"intent": "Feedback",
"score": "1"
}]
}
{
"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!"
}
第一次查询结果
db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0})
{
"currentStep" : [
"flowkuec8ta1o"
]
}
db.getCollection('messagelogs').find({'previousStep':'flow1pemwl7ws'},{'userMessage':1})
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Liked It :)",
"intents": [{
"_id": ObjectId("5cec2dc69b806c4a00f91f17"),
"intent": "General_Positive_Feedback",
"score": "0.9774518966674806"
}]
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Just Okay, Could Be better",
"intents": [{
"_id": ObjectId("5cec2f1a9b806c4a00f91f41"),
"intent": "General_Positive_Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "ILikedIt#1",
"intents": [{
"_id": ObjectId("5cec2dd29b806c4a00f91f19"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "JustOkayCouldBeBetter#1",
"intents": [{
"_id": ObjectId("5cec2f2b9b806c4a00f91f43"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "I'mGivingAPositiveFeedback",
"intents": [{
"_id": ObjectId("5ced84e2fdf046078c85d9cb"),
"intent": "Feedback",
"score": "1"
}]
}
{
"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!"
}
我正在第二次查询中使用第一个查询结果“flowkuec8ta1o”(查询1结果中的“currentStep”用作下面第二次查询中键“previousStep”的值):
第二次查询:
db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0})
{
"currentStep" : [
"flowkuec8ta1o"
]
}
db.getCollection('messagelogs').find({'previousStep':'flow1pemwl7ws'},{'userMessage':1})
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Liked It :)",
"intents": [{
"_id": ObjectId("5cec2dc69b806c4a00f91f17"),
"intent": "General_Positive_Feedback",
"score": "0.9774518966674806"
}]
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Just Okay, Could Be better",
"intents": [{
"_id": ObjectId("5cec2f1a9b806c4a00f91f41"),
"intent": "General_Positive_Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "ILikedIt#1",
"intents": [{
"_id": ObjectId("5cec2dd29b806c4a00f91f19"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "JustOkayCouldBeBetter#1",
"intents": [{
"_id": ObjectId("5cec2f2b9b806c4a00f91f43"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "I'mGivingAPositiveFeedback",
"intents": [{
"_id": ObjectId("5ced84e2fdf046078c85d9cb"),
"intent": "Feedback",
"score": "1"
}]
}
{
"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!"
}
第二次查询结果:
db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0})
{
"currentStep" : [
"flowkuec8ta1o"
]
}
db.getCollection('messagelogs').find({'previousStep':'flow1pemwl7ws'},{'userMessage':1})
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Liked It :)",
"intents": [{
"_id": ObjectId("5cec2dc69b806c4a00f91f17"),
"intent": "General_Positive_Feedback",
"score": "0.9774518966674806"
}]
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Just Okay, Could Be better",
"intents": [{
"_id": ObjectId("5cec2f1a9b806c4a00f91f41"),
"intent": "General_Positive_Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "ILikedIt#1",
"intents": [{
"_id": ObjectId("5cec2dd29b806c4a00f91f19"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "JustOkayCouldBeBetter#1",
"intents": [{
"_id": ObjectId("5cec2f2b9b806c4a00f91f43"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "I'mGivingAPositiveFeedback",
"intents": [{
"_id": ObjectId("5ced84e2fdf046078c85d9cb"),
"intent": "Feedback",
"score": "1"
}]
}
{
"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(其中“currentStep”取自): 附加示例文档2(其中从查询1获取的“currentStep”值用作第二个查询中的“previousStep”) 样本输出:
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
根据@Fanparks请求添加示例文档:以下两个文档与第一个查询匹配(以获取“currentStep”)
文件1:
db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0})
{
"currentStep" : [
"flowkuec8ta1o"
]
}
db.getCollection('messagelogs').find({'previousStep':'flow1pemwl7ws'},{'userMessage':1})
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Liked It :)",
"intents": [{
"_id": ObjectId("5cec2dc69b806c4a00f91f17"),
"intent": "General_Positive_Feedback",
"score": "0.9774518966674806"
}]
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Just Okay, Could Be better",
"intents": [{
"_id": ObjectId("5cec2f1a9b806c4a00f91f41"),
"intent": "General_Positive_Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "ILikedIt#1",
"intents": [{
"_id": ObjectId("5cec2dd29b806c4a00f91f19"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "JustOkayCouldBeBetter#1",
"intents": [{
"_id": ObjectId("5cec2f2b9b806c4a00f91f43"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "I'mGivingAPositiveFeedback",
"intents": [{
"_id": ObjectId("5ced84e2fdf046078c85d9cb"),
"intent": "Feedback",
"score": "1"
}]
}
{
"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!"
}
文件2:
db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0})
{
"currentStep" : [
"flowkuec8ta1o"
]
}
db.getCollection('messagelogs').find({'previousStep':'flow1pemwl7ws'},{'userMessage':1})
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Liked It :)",
"intents": [{
"_id": ObjectId("5cec2dc69b806c4a00f91f17"),
"intent": "General_Positive_Feedback",
"score": "0.9774518966674806"
}]
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Just Okay, Could Be better",
"intents": [{
"_id": ObjectId("5cec2f1a9b806c4a00f91f41"),
"intent": "General_Positive_Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "ILikedIt#1",
"intents": [{
"_id": ObjectId("5cec2dd29b806c4a00f91f19"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "JustOkayCouldBeBetter#1",
"intents": [{
"_id": ObjectId("5cec2f2b9b806c4a00f91f43"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "I'mGivingAPositiveFeedback",
"intents": [{
"_id": ObjectId("5ced84e2fdf046078c85d9cb"),
"intent": "Feedback",
"score": "1"
}]
}
{
"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!"
}
以下文档与第二个查询匹配(其中“previousStep”与从第一个查询中获取的“currentStep”匹配)文件1:
db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0})
{
"currentStep" : [
"flowkuec8ta1o"
]
}
db.getCollection('messagelogs').find({'previousStep':'flow1pemwl7ws'},{'userMessage':1})
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Liked It :)",
"intents": [{
"_id": ObjectId("5cec2dc69b806c4a00f91f17"),
"intent": "General_Positive_Feedback",
"score": "0.9774518966674806"
}]
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Just Okay, Could Be better",
"intents": [{
"_id": ObjectId("5cec2f1a9b806c4a00f91f41"),
"intent": "General_Positive_Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "ILikedIt#1",
"intents": [{
"_id": ObjectId("5cec2dd29b806c4a00f91f19"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "JustOkayCouldBeBetter#1",
"intents": [{
"_id": ObjectId("5cec2f2b9b806c4a00f91f43"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "I'mGivingAPositiveFeedback",
"intents": [{
"_id": ObjectId("5ced84e2fdf046078c85d9cb"),
"intent": "Feedback",
"score": "1"
}]
}
{
"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!"
}
文件2:
db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0})
{
"currentStep" : [
"flowkuec8ta1o"
]
}
db.getCollection('messagelogs').find({'previousStep':'flow1pemwl7ws'},{'userMessage':1})
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Liked It :)",
"intents": [{
"_id": ObjectId("5cec2dc69b806c4a00f91f17"),
"intent": "General_Positive_Feedback",
"score": "0.9774518966674806"
}]
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Just Okay, Could Be better",
"intents": [{
"_id": ObjectId("5cec2f1a9b806c4a00f91f41"),
"intent": "General_Positive_Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "ILikedIt#1",
"intents": [{
"_id": ObjectId("5cec2dd29b806c4a00f91f19"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "JustOkayCouldBeBetter#1",
"intents": [{
"_id": ObjectId("5cec2f2b9b806c4a00f91f43"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "I'mGivingAPositiveFeedback",
"intents": [{
"_id": ObjectId("5ced84e2fdf046078c85d9cb"),
"intent": "Feedback",
"score": "1"
}]
}
{
"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!"
}
文件3:
db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0})
{
"currentStep" : [
"flowkuec8ta1o"
]
}
db.getCollection('messagelogs').find({'previousStep':'flow1pemwl7ws'},{'userMessage':1})
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Liked It :)",
"intents": [{
"_id": ObjectId("5cec2dc69b806c4a00f91f17"),
"intent": "General_Positive_Feedback",
"score": "0.9774518966674806"
}]
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Just Okay, Could Be better",
"intents": [{
"_id": ObjectId("5cec2f1a9b806c4a00f91f41"),
"intent": "General_Positive_Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "ILikedIt#1",
"intents": [{
"_id": ObjectId("5cec2dd29b806c4a00f91f19"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "JustOkayCouldBeBetter#1",
"intents": [{
"_id": ObjectId("5cec2f2b9b806c4a00f91f43"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "I'mGivingAPositiveFeedback",
"intents": [{
"_id": ObjectId("5ced84e2fdf046078c85d9cb"),
"intent": "Feedback",
"score": "1"
}]
}
{
"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!"
}
文件4:
db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0})
{
"currentStep" : [
"flowkuec8ta1o"
]
}
db.getCollection('messagelogs').find({'previousStep':'flow1pemwl7ws'},{'userMessage':1})
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Liked It :)",
"intents": [{
"_id": ObjectId("5cec2dc69b806c4a00f91f17"),
"intent": "General_Positive_Feedback",
"score": "0.9774518966674806"
}]
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Just Okay, Could Be better",
"intents": [{
"_id": ObjectId("5cec2f1a9b806c4a00f91f41"),
"intent": "General_Positive_Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "ILikedIt#1",
"intents": [{
"_id": ObjectId("5cec2dd29b806c4a00f91f19"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "JustOkayCouldBeBetter#1",
"intents": [{
"_id": ObjectId("5cec2f2b9b806c4a00f91f43"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "I'mGivingAPositiveFeedback",
"intents": [{
"_id": ObjectId("5ced84e2fdf046078c85d9cb"),
"intent": "Feedback",
"score": "1"
}]
}
{
"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!"
}
所需输出:
db.getCollection('messagelogs').find({'intents.intent':'General_Positive_Feedback'},{'currentStep':1,'_id':0})
{
"currentStep" : [
"flowkuec8ta1o"
]
}
db.getCollection('messagelogs').find({'previousStep':'flow1pemwl7ws'},{'userMessage':1})
{
"_id" : ObjectId("5cec2dd29b806c4a00f91f18"),
"userMessage" : "ILikedIt#1"
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Liked It :)",
"intents": [{
"_id": ObjectId("5cec2dc69b806c4a00f91f17"),
"intent": "General_Positive_Feedback",
"score": "0.9774518966674806"
}]
}
{
"currentStep": [
"flowkuec8ta1o"
],
"previousStep": [
"conditioncplwf7pw1",
"condition00yokr6jv",
"conditionzq9koi6i3"
],
"userMessage": "Just Okay, Could Be better",
"intents": [{
"_id": ObjectId("5cec2f1a9b806c4a00f91f41"),
"intent": "General_Positive_Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "ILikedIt#1",
"intents": [{
"_id": ObjectId("5cec2dd29b806c4a00f91f19"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "JustOkayCouldBeBetter#1",
"intents": [{
"_id": ObjectId("5cec2f2b9b806c4a00f91f43"),
"intent": "Feedback",
"score": "1"
}]
}
{
"currentStep": [],
"previousStep": [
"flowkuec8ta1o"
],
"userMessage": "I'mGivingAPositiveFeedback",
"intents": [{
"_id": ObjectId("5ced84e2fdf046078c85d9cb"),
"intent": "Feedback",
"score": "1"
}]
}
{
"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!"
}
您可以使用下面的聚合
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"
}}
])
请解释为什么要合并这两个查询?您的上述两个问题之间有什么关系?我看不到
我正在使用第一个查询结果'flowkuec8ta1o'进行第二次查询
。你在第二部中在哪里用的?发布一些样本集和您想要的输出。@Fanpark我已根据您的要求更新了问题。请看一看,如果在as
表达式中找到多个上一步
,那么它可能会给您多条记录,因为我们在这里做的下一步是$unwind
。更新了我的回答您能用示例文档和输出解释一下吗,你能看一下这个问题吗?:我想知道在两个日期之间提取文档的正确方法。你能看一下吗?