mongodb $not _id

前端 未结 2 902
南笙
南笙 2021-01-17 11:49

I need a way to search but not include an _id which is already on the screen in front of the user. For example, I have 3 pet profiles one which the user is already viewing.<

2条回答
  •  逝去的感伤
    2021-01-17 12:17

    You need to do a $ne (not equal) to make sure the current pet you are viewing is excluded from the search by owner.

    Example in the mongo shell:

    var viewingPetId = ObjectId("515535b6760fe8735f5f6899");
    var ownerId = ObjectId("515535ba760fe8735f5f689a");
    
    db.mypet.find(
        {
            _id: { $ne: viewingPetId },
            owner: ownerId
        }
    )
    

提交回复
热议问题