How do I compare two nested documents using mongo_dart

129 Views Asked by At

Here is my database entry structure, it has a nested document called friends, I want to compare two different _id's friends list in dart using mongo_dart

{
"_id" : ObjectId("60ae06074e162995281b4666"),
"email" : "[email protected]",
"emailverified" : false,
"username" : "[email protected]",
"displayName" : "complete n00b",
"phonenumber" : "",
"dob" : "",
"points" : 0,
"friends" : [
    {
        "username" : "[email protected]",
        "sent" : ISODate("2021-05-26T10:01:30.616Z")
    },
    {
        "username" : "[email protected]",
        "sent" : ISODate("2021-05-26T10:43:16.822Z")
    }
]

}

Here is my code, but I am not getting any returns

Future<Map> commonFriends(store, myObjectId, theirObjectId) async {
  var commonList = await store.aggregate([
    {
      '\$project': {
        'friends': 1,
        'commonToBoth': {
          '\$setIntersection': [
            {'_id': myObjectId, 'friends': '\$username'},
            {'_id': theirObjectId, 'friends': '\$username'}

          ]
        },
      }
    }
  ]);
  return commonList;
}

I am getting an error from db.dart which is apart of mongo_dart package. The error is "Exception has occurred. Map (4 items)"

0

There are 0 best solutions below