简体   繁体   中英

Mongoose find all except one in aggregate users

        User.aggregate([{
            $lookup: {
                from: "boughtitems", // collection name in db
                localField: "username",
                foreignField: "username",
                as: "boughtitems"
            }
        }]).then((users) => { 

Can someone help me to write this code so that I can find all users except myself ? Every logged in users has it's own session so I can find them by ID like so: User.findById(req.session.userId)

You can use $ne aggregation operator

User.aggregate([
  { "$match": { "_id": { "$ne": mongoose.Types.ObjectId(yourLoggedInId) }}},
  { "$lookup": {
    "from": "boughtitems",
    "localField": "username"
    "foreignField": "username",
    "as": "boughtitems"
  }}
])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM