diff --git a/mongo/add-field-with-default-value.js b/mongo/add-field-with-default-value.js new file mode 100644 index 0000000..b5d6a0e --- /dev/null +++ b/mongo/add-field-with-default-value.js @@ -0,0 +1,5 @@ +db.getCollection('myCollection').update( + {$or:[{"name":null},{"name":{$exists:false}}]}, + {$set:{"name":"default, value"}}, + {multi:true} +); diff --git a/mongo/collectionNames-and-count.js b/mongo/collectionNames-and-count.js new file mode 100644 index 0000000..1705c27 --- /dev/null +++ b/mongo/collectionNames-and-count.js @@ -0,0 +1,18 @@ +var result = []; +db.getCollectionInfos().forEach(function(collection) { + var collectionResult = {name: collection.name, count: db[collection.name].count()} + result.push(collectionResult); +}); +printjson(result); + +// resulting in: +// [ +// { +// "name" : "test", +// "count" : 1 +// }, +// { +// "name" : "test2", +// "count" : 3 +// } +// ]