Esta publicación servirá para ir colocando fragmentos de consultas que en algún momento me fueron útiles para usarlo en MongoDB Fragmento 1: Se requería obtener todos los elementos que dentro de un atributo que es de tipo array(arreglo) coincida con type:"fire" Fragmento 2: Se requería actualizar el atributo " lastUpdate " de toda la colección donde la edad sea mayor o igual a 15.
Esta publicación servirá para ir colocando fragmentos de consultas que en algún momento me fueron útiles para usarlo en MongoDB
Fragmento 1: Se requería obtener todos los elementos que dentro de un atributo que es de tipo array(arreglo) coincida con type:"fire"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
var array = {id :"item1", images:[{id:"img1",type:"fire"},{id:"img2",type:"fire"},{id:"img3",type:"water"}, | |
{id:"img4",type:"whater"},{id:"img5",type:"fire"},{id:"img6",type:"land"} | |
]} | |
*/ | |
db.getCollection("images").find({images: {$elemMatch: {type:'fire'}}}) | |
/* | |
Result: | |
[{id:"img1",type:"fire"},{id:"img2",type:"fire"},{id:"img5",type:"fire"}] | |
*/ |
Fragmento 2: Se requería actualizar el atributo "lastUpdate" de toda la colección donde la edad sea mayor o igual a 15.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.getCollection("images").update( | |
{edad:{$gte:15}}, | |
{$set: {lastUpdate: null}}, | |
{ | |
multi: true, | |
} | |
); |
Comentarios
Publicar un comentario