Custom query operator

Hello! This is my first post and I am extremely frustrated trying to solve this issue.

I have a document with a Date string.
I want to query documents that fall on a particular day of the week by checking this date property.
This would require me to manipulate the property before applying a logical operator that checks equivalency.

I found the $where and $function operators that seem to allow this custom functionality:
image

However, neither of these methods are functional and for the life of me I cannot figure out why. I have also tried "Model.aggregate({ $match: { $function: … " instead of .find() with the same problems.

I am trying to avoid needing separate properties for Date/Day/Hour information but it’s looking like I have no other choice

Hello :wave: @Kevin_Rancourt,

Welcome to the MongoDB Community forums :sparkles:

Based on your shared information I presume the document is as follows:

{
  "_id": ObjectId("123456789012345678901234"),
  "title": "Sample Date Document",
  "someDate": "2022-04-17T12:30:00Z"
}

Considering, you want to query for documents with a “someDate” property that falls on a Monday, I have written a MongoDB aggregation pipeline using $match, $toDate and $dayOfWeek to retrieve documents with a “someDate” property that falls on a ‘Monday’:

const agg = [
  {
    '$match': {
      '$expr': {
        '$eq': [
          { '$dayOfWeek': {'date': {'$toDate': '$someDate' } } }, 2
        ] }
    }}
];
const client = await MongoClient.connect(
  'mongodb://localhost:27017/',
  { useNewUrlParser: true, useUnifiedTopology: true }
);
const coll = client.db('sampleDb').collection('sampleColl');
const result = await coll.aggregate(agg).toArray();
await client.close();

It uses the $match stage to filter documents based on a logical expression, defined using the $expr operator. Inside that, the $dayOfWeek operator is used to extract the day of the week from the “someDate” after converting it to a Date object using $toDate, and the resulting value is compared to the numeric value for Monday, which is 2.

I hope this helps. If you have any further questions or require additional assistance, please provide sample documents and your expected output.

Regards,
Kushagra

Thank you very much for your help.

If I could trouble you for just one more thing.

In the following example, I am trying to filter a collection based on a property the client determines. All documents that include a specific user account
This would require the object to follow the format { “friends.user”: {$in: [ _id ] } }

The client needs this field to be populated in case they decide to edit it.
I cannot refer to the field as obj.friends.user.$in because the singular field is called “friends.user”, it is not a nested object like {friends: {user: … } }
In which case, the property would be referenced using : obj[“friends.user”]

This is a problem because the populate method requires the path as a string, and I cannot have a string within a string such as: Model.populate(“obj[‘friends.user’].$in”)

Would there be a way for this format to function the way I intend? Or is there a better way to format the filter object in the first place so that this isn’t even an issue?
Any help would be greatly appreciated.

Hello @Kevin_Rancourt,

Can you please open a new topic with the sample documents, code snippets, the expected output, and the versions of MongoDB and Node.js you are using?

Best,
Kushagra