Arrgregate $out mongodb insert for $out failed duplicate Error

后端 未结 2 692
清歌不尽
清歌不尽 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:46

    Your $lookup stage simply returns multiple entries in the user field which then get flattened into individual subdocuments that have the same _id value. You can validate this by replacing the $out stage with a $match instead:

    db.col1.aggregate([{
        $match: { _id: ObjectId('5b16305d145a5117552836ec') }
    }
    

    The key here is to understand how $unwind works. Imagine you have the following document:

    {
        _id: 1,
        users: [ 'a', 'b' ]
    }
    

    Once you $unwind this you will get the following two documents:

    {
        _id: 1,
        users: 'a'
    },
    {
        _id: 1,
        users: 'b'
    }
    

    So you get the same _id value for two different documents!

    Now, when you specify _id: 0 in your projection you remove the _id field from all documents which will cause MongoDB to automatically create new _ids which of course then works.

    0 讨论(0)
  • 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"
    }
    
    0 讨论(0)
提交回复
热议问题