I'd like to $push the variable content into the database content if the content.hash isn't already in the database. I don't want to store information again unnecessarily.
return shops.updateAsync({
"user": user
}, {
"$push": {
"content": content
}
})
I have a content object with the following params.
content = {
"type": "csv",
"name: "my-csv.csv",
"content": csvContent,
"date": new Date(),
"hash": hash.digest('hex')
}
I don't want to ever insert the same CSV into the array. I'd like to ensure that there's only one by checking the hash, if there's a content object in the array with the hash it wouldn't upload or overwrite it.
Here's what I got so far, it's 3 requests :(
return users.findOneAsync({
"user": user,
"content.hash": content.hash,
}).then(function(exists){
if(!exists) return false
return users.updateAsync({
"user": user
}, {
"$pull": { "content": { "hash": content.hash } },
})
}
}).then(function(){
return users.updateAsync({
"user": user,
}, {
"$push": {"content": content}
})
})
contentarray, but in your code example you're overwriting it with new object. Which is the right behavior?