I am new to MongoDB, my native language is Spanish. So I'm not sure how to find what I need.
I will try to make myself understandable.
my schema:
let noticiaSchema = new Schema({
titulo: {
type: String,
required: [true, 'El titulo de la noticia es requerido']
},
cuerpo_noticia: {
type: String,
required: [true, 'El cuerpo de la noticia es necesario']
},
publicacion_noticia: {
type: Date,
default: new Date()
},
actualizacion_noticia: {
type: Date,
default: new Date()
},
likes: {
type: Array,
default: []
},
foto: {
type: String
},
dislikes: {
type: Array,
default: []
}
})
Sample Doc :
{
"_id" : ObjectId("5e81868faf9d6e084cc60cc8"),
"titulo" : "fdsfsfs",
"cuerpo_noticia" : "fdsfsfs",
"actualizacion_noticia" : ISODate("2020-03-30T05:41:35.144Z"),
"foto" : "14335143.png",
"publicacion_noticia" : ISODate("2020-03-30T05:41:12.997Z"),
"likes" : [
"5e7ffb641650a326dcc1e1c7"
],
"dislikes" : [],
"__v" : 0
}
I'm basically trying to query for an array of elements called likes
, if an element is contained in this array I would want to return true / false
on a new field.
Below is what I've tried, but it only returns the documents where the element exists in likes
.
//5e7ffb641650a326dcc1e1c7 element to search
Noticia.aggregate([
{
$match: {
"likes": { "$in": ["5e7ffb641650a326dcc1e1c7"] },
//likes is the array field with elements to search
}
},
{
$project: {
titulo: "$titulo"
}
}
], (err, trans) => {
})
I want it to return all my docs but with a field that tells me if this element is contained or not.
Finally, I want to know if there is a way to return the result by creation date, that is .sort {_id: -1}