i want to retrieve conversation between two user sorting by the newest with the user sender field my message and user schema :
JavaScript
x
32
32
1
const messageSchema = new mongoose.Schema({
2
from:{
3
type: mongoose.Schema.Types.ObjectId,
4
ref:'User'
5
},
6
to:{
7
type: mongoose.Schema.Types.ObjectId,
8
ref:'User'},
9
date:{
10
type:String, default:new Date()
11
},
12
message: {type:String},
13
seen:{type:Boolean,default:false}
14
15
},{timestamps:true})
16
17
const userSchema = new mongoose.Schema({
18
name : {
19
type: String,
20
required: true,
21
},
22
23
lastname : {
24
type: String,
25
required: true,
26
},
27
firstname : {
28
type: String,
29
required: true,
30
},
31
})
32
Advertisement
Answer
i found a solution :
JavaScript
1
6
1
const conversation = await Message.find({$or: [
2
{'from': req.user._id,'to': userId},
3
{'from':userId,'to': req.user._id}
4
]},
5
).populate('from','name image ').limit(20);
6