I want a group of random data of a Connection type schema. But I want to filter the data in accordance with a group of array 'isConnections' which is a collection of userID's, so that the fetched data does not include any document where its userID is in the 'isConnections' array.
Initially, I tried using the following logic, and many others that I found over stack overflow, but none worked.
"isConnection": [
"62adca9063bc6adb05d79db8",
"62af5f3a3920076f461d8a3f",
"62bfd0935d52cdde5a4e89a9",
"62b3bb29342af89edcd11264"
]
let Connections = await ConnectionModel.aggregate([
{ $match: { UserID: { $ne: isConnection } } },
{ $sample: { size: 10 } }
])
CodePudding user response:
$ne
is looking for an exact match. you want to be using $nin ( not in ), like so:
let Connections = await ConnectionModel.aggregate([
{ $match: { UserID: { $nin: isConnection } } },
{ $sample: { size: 10 } }
])
Also just make sure UserID
is actually string
type and not ObjectID
, otherwise you'll have to convert the input array as well.