Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- userId i am getting from the route
- Message.aggregate(
- [
- { $match: { $or: [{ 'from.userId': userId }, { 'to.userId': userId }] } },
- // { $project: {...FIELDS}}, ///what to use here
- { $sort: { timestamp: -1 } },
- {
- $group: {
- "_id": {
- 'fromuserId': userId,//confusion here
- // 'toUserId': userId,
- // 'to.user'
- },
- // 'userId': { $first: '$from' },
- // 'userId': { $first: '$to' },
- msg: { $first: '$msg' },
- timestamp: { $first: '$timestamp' }
- }
- }
- ]
- ).exec((err, results) => {
- if (err) {
- console.log('err===>', err);
- }
- console.log('results|||', results)
- })
- my schema is this :
- const MessageSchema = new mongoose.Schema({
- to: {
- userId: {
- type: mongoose.Schema.Types.ObjectId,// to be sent from client side
- ref: "User",
- required: true
- },
- name: {
- type: String,
- },
- pictureUrl: {
- type: String,
- }
- },
- from: {
- userId: {
- type: mongoose.Schema.Types.ObjectId,// to be sent from server side
- ref: "User",
- required: true
- },
- name: {
- type: String,
- },
- pictureUrl: {
- type: String,
- }
- },
- message: {
- type: String,
- required: true
- }
- },
- {
- timestamps: true
- }
- );
Add Comment
Please, Sign In to add comment