Mongoose timestamps aren't set or updated when using $set - mongodb

I'm using Mongoose timestamps in my schema:
{ timestamps: true }
but then when I use $set on my document like so:
$set: {
"person.name": "Owen",
},
The timestamps (createdAt & updatedAt) aren't set or updated.
but when I use $set like so:
$set: {
person: {
name: "Owen"
}
}
then it does work.
Is this the expected behaviour? And is there anything I can do about it?

Related

MONGO : How to update a field using value of another field

I have a collection of people:
{
name: Juan,
age: 21
}
and now , I want to achieve the following:
{
name: Juan,
age: 21,
name_reply:Juan
}
I have tried using:
db.people.updateMany(
{"name_reply":null},
[{$set:{name_reply:"$name"}}]
);
but the following appears.
Expected type object but found array.
How could I update a mongo field using the value of another field?
db.collection.update({
"name_reply": null
},
[
{
"$set": {
"name_reply": "$name"
}
}
],
{
multi: true
})
But indeed this is only for mongodb version >=4.2 where you can provide aggregation pipeline to the update query
playground
for earlier versions you can check this answer here
something like this could do the job most probably:
db.collection.find({"name_reply":null}).snapshot().forEach(
function (doc) {
doc.name_reply = doc.name;
db.collection.save(doc);
}
);

How to ignore schema default value when querying in mongoose?

eg.
created: {
type: Date,
default: Date.now,
}
, but in mongodb there exists some old data without "created" field. How can I get real data but not Date.now.
We can use $exists here to fetch the new documents which will have the created field.
Mongo Shell Query
db.collection.find( { created: { $exists: true} } )

What is the better implementation? MongoDb Query

I need some help with MongoDb. I need to check if an object exists in the database. If it's true, then I need to check if this object has a specific element into array (Products). If not, I need to create this object(Order) with this element(Cookie) in to array(Products).
Example data:
Order {
_id: ObjectId("580bc55f54101f1d18152d88"),
code: "AVG223424",
products: [
{
name: "Cookie"
},
{
name: "Soda"
}
]
}
Finally, what is the better implementation?
Assuming you are using a collection with the name 'Orders'
db
.Orders
.update({
code: "12345"
}, {
$addToSet: {
products: {
name: "Cookie"
}
},
$setOnInsert: {
code: "12345"
}
}, {
upsert: true
});
This query looks for a document with the same 'code,' and if found, will add the object '{name: "Cookie"}' if there is no other Object with the same key/val pairs. If the document does not exist, the '$setOnInsert' command will set the specified fields only if a new document is created.

How to add ObjectId property to each object of an array

I am have manually created an object in a Mongo collection:
{
"messages": [
{
"url":"http://test.test.com",
"message":"test message"
}
],
....other properties
}
I would like to add an _id:ObjectId() to each item of my messages array and for each document in the collection.
I tried:
collection.update({}, {
$set: {
'messages.$._id': ObjectId(),
},
}, { multi: true }
but this is not working. The Id is getting added when I add new ones going through Mongoose, but these were manually entered into mongo. Any help is appreciated.
Your syntax is correct, but in order to use the $ positional operator the array field must appear as part of the query document, check in documentation.
Try this:
db.collection.update({messages: {$exists: true }},
{$set: { 'messages.$._id': ObjectId() } },
{multi: true}
)

Mongodb cursor to Json

So my problem is that I cant push a result of findOne() method to array in document.
For example, I want to add another user to some array (project participants) like this :
var user =db.users.findOne({_id:"123456"})
db.projects.update({_id:'abcde'},{$set:{$push:{participants:user}}})
how can I fix that ?
You need your update query like so:
db.test.update({
_id: 'abcde'
},
{
$push: {
participants: { name: 'Bob'}
}
})