MongoDb: Difference between $push/$addtoset

后端 未结 6 692
迷失自我
迷失自我 2020-12-08 09:06

I read the documentation in the MongoDb and I used a simple proves and I only look that: Push is sorting the array but addtoSet isn\'t it.

For me visual

6条回答
  •  余生分开走
    2020-12-08 09:22

    $push: Inserts the value to an array in the resulting document. eg;

    db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])
    

    $addToSet: Inserts the value to an array in the resulting document but does not create duplicates. eg;

    db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])
    

提交回复
热议问题