Arrgregate $out mongodb insert for $out failed duplicate Error

后端 未结 2 703
清歌不尽
清歌不尽 2021-01-29 00:56

Query1: i was trying to combine two collection with selected field of both collections using mongodb Aggregate $lookup as follows

 db.col1.aggregate([
   {
              


        
2条回答
  •  滥情空心
    2021-01-29 01:57

    Ok for duplicated id explanation. In addition, if you want to keep user._id, add id:"$_id" to your last project stage.

    But for the second query with _id:0, what do you mean by 'no error and no output :

    • If your query is running well, results will be inserted in a new collection called 'new_col', so you have to run db.getCollection("new_col").find({}); to retrieve them. Nothing in console with $out stage!
    • If your new_col collection doesn't exist or is empty : your query is returning nothing, no matter of $out stage. In this case, please provide col1 and col2 document samples

    ---EDIT--- With these data :

    col1 : 
    { 
    "_id" : 1.0, 
    "field1" : "a", 
    "field2" : "b"
    }
    col2 : 
    { 
    "_id" : 1.0, 
    "field1" : "a", 
    "field2" : "c"
    }
    

    the right query to achieve what you need is the following :

    db.col1.aggregate(
    [
        // Stage 1
        {
            $lookup: {
                 from: "col2",
                      localField: "field1",    
                      foreignField: "field1", 
                      as: "user"
            }
        },
        // Stage 2
        {
            $unwind: {
                path : "$user",
    
            }
        },
        // Stage 3
        {
            $project: {
               "userfield": "$user.field1",field1:1
            }
        },
    
    ]
    );
    

    Will output :

    { 
        "_id" : 1.0, 
        "field1" : "a", 
        "userfield" : "a"
    }
    

提交回复
热议问题