How do I only return SOME fields ($project) from Mongodb aggregation, while also using $match, $lookup AND $filter

匆匆过客 提交于 2020-06-27 05:50:25

问题


I am VERY close to getting what I want out of this query... but I only want SOME of the fields returned and right now it is returning all of them

NOTE: This is a refinement : I am now asking how to return only certain fields, while my similar question asks how to return the data between a start and end date

In addition, can somebody please please provide an answer using the MongoDB Playground with MY data sets so I can try it out... I can't quite figure out how to "name" the data sets so they work in the playground !

Register Schema

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const RegisterSchema = new Schema({
    userId: {type: Schema.Types.ObjectId, required: true},
    accessToken: {type:String, required: true, default: null},
})
module.exports = Register = mongoose.model( 'register', RegisterSchema)

Here is some register data

[
  {
    "_id": "5eac9e815fc57b07f5d0d29f",
    "userId": "5ea108babb65b800172b11be",
    "accessToken": "111"
  },
  {
    "_id": "5ecaeba3c7b910d3276df839",
    "userId": "5e6c2dddad72870c84f8476b",
    "accessToken": "222"
  }
]

The next document contains data that is related to the Register schema via the accessToken

Notifications

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const NotificationSchema = new Schema({
    accessToken: {type:String, required: true},
    summaryId: {type:Number, required: true},
    dateCreated: {type: Date, default: Date.now},

    // I don't want these returned in the final results
    dontWantThis1: {type:Number, required: true},
    dontWantThis2: {type:Number, required: true},
})
module.exports = Notification = mongoose.model( 'notification', NotificationSchema)

Here is some notification data

[{
    "_id": "5ebf0390c719e60004f42e74",
    "accessToken": "111",
    "summaryId": 1111,
    "dontWantThis1": 61,
    "dontWantThis2": 62,
    "dateCreated": "2020-04-17T00:00:00.000+00:00" }, 
  {
    "_id": "6ebf0390c719e60004f42e76",
    "accessToken": "222",
    "summaryId": 2221,
    "dontWantThis1": 71,
    "dontWantThis2": 72,
    "dateCreated": "2020-04-18T00:00:00.000+00:00" },
  {
    "_id": "6ebf0390c719e60004f42e78",
    "accessToken": "111",
    "summaryId": 1112,
    "dontWantThis1": 611,
    "dontWantThis2": 622,
    "dateCreated": "2020-05-25T00:00:00.000+00:00" },
  {
    "_id": "6ebf0390c719e60004f42e80",
    "accessToken": "222",
    "summaryId": 2222,
    "dontWantThis1": 711,
    "dontWantThis2": 722,
    "dateCreated": "2020-05-26T00:00:00.000+00:00" }
]

Works, returns data between the two dates, but

This code returns everything, including the 'dontWantThis1' and 'dontWantThis2'

NOTE

I do not want the fields prefaced with 'dontWantThis' - but that is only to show which ones I don't want... I don't literally want to exclude fields prefaced with 'dontWantThis' ..... they could be named 'foo' or 'apple' or 'dog' they are just named that way to indicate that I don't want them


        // make sure the input dates are REALLY date objects
        // I only want to see notifications for the month of May (in this example)
        var dateStart = new Date('2020-05-01T00:00:00.000+00:00');
        var dateEnd = new Date('2020-05-30T00:00:00.000+00:00');     

        var match = {$match: { userId: mongoose.Types.ObjectId(userId) } };

        var lookup ={
            $lookup:
            {
                from: "my_Notifications",
                localField: "accessToken",
                foreignField: "accessToken",
                as: "notifications"
            }
        };

        var dateCondition = { $and: [
            { $gte: [ "$$item.dateCreated", dateStart ] },
            { $lte: [ "$$item.dateCreated", dateEnd ] }
          ]}  

        var project = {
            $project: {
                notifications: {
                    $filter: {
                    input: "$notifications",
                    as: "item",
                    cond: dateCondition
                    } } }
        };

        var agg = [
            match,
            lookup,
            project
        ];

        Register.aggregate(agg)
        .then( ..... )

Try 1

I thought I could do something like this, but it still returns ALL of the notification fields

        var project = {
            $project: {
                "_id": 1,
                "userId": 1,
                "accessToken":1,
                "count":{$size:"$notifications"},
                "notifications._id":1,
                "notifications.summaryId": 1,
                "notifications.dateCreated":1,

                notifications : {
                    $filter: {
                    input: "$notifications",
                    as: "item",
                    cond: dateCondition
                    },
            }}
        };

SOLUTION

I created another projection and added that to the pipeline:

        var project2 = {
            $project: {
                "_id": 1,
                "userId": 1,
                "accessToken":1,
                "count":{$size:"$notifications"},
                "notifications._id":1,
                "notifications.summaryId": 1,
                "notifications.dateCreated":1,
                "notifications.dateProcessed":1,
            }
        };


        var agg = [
            match,
            lookup,
            project,
            project2,
        ];

Thanks!!


回答1:


https://stackoverflow.com/users/6635464/ngshravil-py was spot on.

I created another projection:

        var project2 = {
            $project: {
                "_id": 1,
                "userId": 1,
                "accessToken":1,
                "count":{$size:"$notifications"},
                "notifications._id":1,
                "notifications.summaryId": 1,
                "notifications.dateCreated":1,
                "notifications.dateProcessed":1,
            }
        };

Then added it to my aggregation pipeline:

        var agg = [
            match,
            lookup,
            project,
            project2,
        ];

Worked ! -- thank you https://stackoverflow.com/users/6635464/ngshravil-py




回答2:


You need to convert notifications.dateCreated to ISODate, as your date is in string, by using $dateFromString and $map operator. i suggest you to do this, because I don't think that you can do date comparison with string formats. Also, make sure that dateStart and dateEnd should also be in ISODate format.

And you need two $project operator in order to achieve this. Also, I don't see any field with userAccessToken, I assume, it's accessToken. Check the below query.

db.Register.aggregate([
  {
    $lookup: {
      from: "my_Notifications",
      localField: "accessToken",
      foreignField: "accessToken",
      as: "notifications"
    }
  },
  {
    $project: {
      "_id": 1,
      "userId": 1,
      "accessToken": 1,
      notifications: {
        $map: {
          input: "$notifications",
          as: "n",
          in: {
            "_id": "$$n._id",
            "summaryId": "$$n.summaryId",
            "dateCreated": {
              $dateFromString: {
                dateString: "$$n.dateCreated"
              }
            }
          }
        }
      }
    }
  },
  {
    $project: {
      "userId": 1,
      "accessToken": 1,
      "notifications": {
        $filter: {
          input: "$notifications",
          as: "item",
          cond: {
            $and: [
              {
                $gte: [
                  "$$item.dateCreated",
                  ISODate("2020-05-24T00:00:00Z")
                ]
              },
              {
                $lte: [
                  "$$item.dateCreated",
                  ISODate("2020-05-26T00:00:00Z")
                ]
              }
            ]
          }
        }
      }
    }
  },
  {
    $set: {
      "count": {
        $size: "$notifications"
      }
    }
  }
])

MongoPlayGroundLink



来源:https://stackoverflow.com/questions/62066169/how-do-i-only-return-some-fields-project-from-mongodb-aggregation-while-also

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!