Reputation: 737
I am trying to do an update only if the date is between two dates with the next sentence:
db.periodo.update(
{"id_linea" : id_linea, "id_contexto" : id_contexto,$min: { "fecha_hora_inicio": fecha_desde }, $max: {"fecha_hora_inicio": fecha_hasta} },
{ $set: { "id_fase": id_fase } },
{ multi: true }
)
My first problem is that my structure is :
id_linea
id_contexto
periodo
id_fase
fecha_hora_inicio
If I want to use as conditions the date inside periodo
is it enough to put "fecha_hora_inicio
" or must I reference with periodos.fecha_hora_inicio
?
When I tried the two sentences I get the following failure:
error: {
"$err" : "Can't canonicalize query: BadValue unknown top level operator: $min",
"code" : 17287
}
I have redone my consult because i find mistake: now my consult is:
db.collection.update(
{"id_linea" : id_linea, "id_contexto" : id_contexto }, { $min: { "periodos.fecha_hora_inicio": fecha_desde }} , {$max: {"periodos.fecha_hora_inicio": fecha_hasta}} ,
{ $set: { "id_fase": id_fase }},
{ multi: true })
}
When i tried in robomongo with values, for example:
db.grupo_periodo.find(
{"id_linea" : 145, "id_contexto" : 151 }, { $min: { "periodos.fecha_hora_inicio": 2015-04-16 16:51:34 }} , {$max: {"periodos.fecha_hora_inicio": 2015-04-27 16:51:34 }}
}
i find the Error: Line 3: Unexpected number
Upvotes: 0
Views: 189
Reputation: 737
if(collection !=null){
db.collection.update(
{"id_linea" : id_linea, "id_contexto" : id_contexto , periodos: { $elemMatch: { "fecha_hora_inicio": { $lte: fecha_hasta }, fecha_hora_fin: { $gt: fecha_desde } }}} ,
{ $set: { "periodos.$.id_fase": id_fase }},
{ multi: true })
}
With this all is perfect now.
Upvotes: 0
Reputation: 14156
Some major edits after my first answer.
The structure of your document in the grupo_periodo
collection is as follows:
{
id_linea: 145,
id_contexto: 151,
periodo: [
{
fecha_hora_inicio: new Date(2014, 10, 10),
id_fase: 'f'
},
{
fecha_hora_inicio: new Date(2014, 9, 10),
id_fase: 'f'
}
]
}
And you want to update the "id_fase" of the "periodo" objects matching some date range.
What you are looking for is called "positional $ update".
Here is a complete example to understand how this work:
db.grupo_periodo.insert({
id_linea: 145,
id_contexto: 151,
periodo: [
{
fecha_hora_inicio: new Date(2014, 10, 10),
id_fase: 'f'
},
{
fecha_hora_inicio: new Date(2014, 9, 10),
id_fase: 'f'
}
]
});
db.grupo_periodo.insert({
id_linea: 145,
id_contexto: 151,
id_fase: 'f',
periodo: [
{
fecha_hora_inicio: new Date(2014, 10, 10),
id_fase: 'f'
},
{
fecha_hora_inicio: new Date(2014, 6, 10),
id_fase: 'f'
}
]
});
db.grupo_periodo.insert({
id_linea: 145,
id_contexto: 151,
id_fase: 'f',
periodo: [
{
fecha_hora_inicio: new Date(2014, 6, 10),
id_fase: 'f'
}
]
});
var query = {
id_linea: 145,
id_contexto: 151,
'periodo.fecha_hora_inicio': {
$gt: new Date(2014, 6, 1),
$lt: new Date(2014, 6, 30)
}
};
//let's update all "periodo" elements in the arrays matching the query:
db.grupo_periodo.update(query, {
$set: {
'periodo.$.id_fase': 'updated'
}
}, {
multi: true
});
//now query:
db.grupo_periodo.find(query)
The result of the last query is:
{
"_id" : ObjectId("5538f5dacae6e0da9298ec4b"),
"id_linea" : 145,
"id_contexto" : 151,
"id_fase" : "f",
"periodo" : [
{
"fecha_hora_inicio" : ISODate("2014-11-10T03:00:00Z"),
"id_fase" : "updated"
},
{
"fecha_hora_inicio" : ISODate("2014-07-10T03:00:00Z"),
"id_fase" : "f"
}
]
}
{
"_id" : ObjectId("5538f5dbcae6e0da9298ec4c"),
"id_linea" : 145,
"id_contexto" : 151,
"id_fase" : "f",
"periodo" : [
{
"fecha_hora_inicio" : ISODate("2014-07-10T03:00:00Z"),
"id_fase" : "updated"
}
]
}
Showing that the specific periodo
documents have the id_fase
updated.
Upvotes: 1