From bb88a817898cc03fdd1a254eb3818d8f3b9b1ae1 Mon Sep 17 00:00:00 2001 From: Mastermindzh Date: Mon, 12 Jul 2021 14:05:44 +0200 Subject: [PATCH] added two mongo examples --- mongo/add-field-with-default-value.js | 5 +++++ mongo/collectionNames-and-count.js | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 mongo/add-field-with-default-value.js create mode 100644 mongo/collectionNames-and-count.js 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 +// } +// ]