I need to save an array in my MondoDB database. I do this without problems with the following code:
My array of objects:
[ { name: '1', type: 'float', value: 89.05, timestamp: 1535440000 },
{ name: '10', type: 'float', value: 19.67, timestamp: 1535440000 },
{ name: '15', type: 'float', value: 1.7, timestamp: 1535440000 },
{ name: '17', type: 'float', value: 2.2, timestamp: 1535440000 } ]
save code:
data_Greenhouse.collection.insert(data_inver, function (err, docs) {
if (err){
res.status(500).send({message: 'Error al guardar en la base de datos'})
} else {
console.log("Multiple documents inserted to Collection Data_Greenhouse");
res.status(200).send({data_Greenhouse: docs})
}
});
I need to check before saving the array if there is already a record for the name field and for the date in my collection. If it exists, do not save it and otherwise save it.
How could I do this?
Greetings, thank you. I hope this is well explained is somewhat confusing.
EDIT01
I edit the Post with a summary of what I need.
I really need to pass the array of objects to my query and check if it exists and generate a new one with the values that are not in the database check.
EDIT02
The code I am using is the following:
data_inver.forEach(elem => {
var Yaexiste = Data_Greenhouse.findOne({ 'name': elem.name,'timestamp':elem.timestamp}); // Realizas la busqueda en la DB para cada objeto sino existe, entonces lo inserta
console.log('mostramos los valores: ' + elem.name +" " + elem.timestamp);
console.log('mostramos los valores: ' + Yaexiste[0]);
if(Yaexiste==undefined){
data_Greenhouse.collection.insert(elem, function (err, docs) {
if (err){
res.status(500).send({message: 'Error al guardar en la base de datos'})
console.log("Error");
} else {
console.log("Guardado");
//res.status(200).send({docs})
}
});
}else {
console.log('Los datos ya existen');
}
});
The response I get is the following:
mostramos los valores: 653 1535440000
mostramos los valores: undefined
Los datos ya existen
mostramos los valores: 657 1518420000
mostramos los valores: undefined
Los datos ya existen
mostramos los valores: 8 1535440000
mostramos los valores: undefined
Los datos ya existen
As you can see, it does not enter theif Yaexiste==undefined
Final solution:
If it behaves correctly I will add it as an answer.
Data_Greenhouse.findOne({ 'name': elem.name ,'timestamp':elem.timestamp}, function (err, user) {
console.log('mostramos los valores: ' + user);
if(user==null){
data_Greenhouse.collection.insert(elem, function (err, docs) {
if (err){
res.status(500).send({message: 'Error al guardar en la base de datos'})
console.log("Error");
} else {
console.log("Guardado");
//res.status(200).send({docs})
}
});
}else {
console.log('Los datos ya existen');
}
});
});
Well indeed you have to create a previous query to see if it exists or not:
There you have the code to search with a callback, as you will see there are the output parameters err and
data
, you have to evaluatedata
since it will tell you if the data you are looking for exists or not.I am writing another answer where I complement your doubt in a comment to a previous answer:
You would have to evaluate data to see what index of that array shows you or contains the records and then do the forEach
You can also be more specific in the query you can do this:
If the table is very large as you tell me, for those you use the filter inside the find method example:
If you look for a record in the table where it has a name of 10, you use this:
data_Greenhouse.find({'collection.name': '10'}, 'name, type value', function (err, data) { if (err) return handleError(err);
With the filter you confirm if there is a record equal to the one you are going to insert
You have an array with objects to insert:
you run that array