Database
db={"friendships":[{"user1":"A","user2":"B","areFriends":true},{"user1":"C","user2":"B","areFriends":true},{"user1":"B","user2":"D","areFriends":true},{"user1":"A","user2":"D","areFriends":true},{"user1":"D","user2":"E","areFriends":true},{"user1":"E","user2":"C","areFriends":true}]}
Query
db.friendships.aggregate([{"$match":{"areFriends":true}},{"$set":{"user1":{"$min":["$user1","$user2"]},"user2":{"$max":["$user1","$user2"]}}},{"$out":{"coll":"friendshipsView"}}])