Skip to content

Instantly share code, notes, and snippets.

@B-Galati
Last active March 4, 2016 10:46
Show Gist options
  • Select an option

  • Save B-Galati/2b9b8ee1b03818f695ea to your computer and use it in GitHub Desktop.

Select an option

Save B-Galati/2b9b8ee1b03818f695ea to your computer and use it in GitHub Desktop.
Simple mongo requests
// MAJ d'un champ de tous les document d'une collection (upsert:false et multi:true)
db.dossier.update(
{},
{$set: {"entreprise.codeNAF": {"code": "NAF"}}},
false,
true
);
// Parcours d'une collection
db.dossier.find().forEach(function(dossier){
print(dossier.id);
dossier.prop = "toto";
db.dossier.save(dossier);
});
// Pour résumer : db.dossier.find("contacts.*.nom": "MARTIN");
// NB : Préciser $where est facultatif
db.dossier.find({
$where: function() {
for (var contactId in this.contacts) {
if (/MARTIN/.test(this.contacts[contactId].nom)) {
return true;
}
}
}
});
// Compter toutes les occurences d'un champ afin de trouver des doublons
db.delegue.aggregate([
{ $group: { // Pour chaque "email" trouvé l'objet suivant est créé
_id: { email: "$email" },
uniqueIds: { $addToSet: "$_id" }, // Contient les _id des doublons trouvés
count: { $sum: 1 }
} },
{ $match: {
count: { $gte: 2 } // Affiche seulement les doublons voire +
} },
{ $sort : { count : -1} }
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment