Last active
June 8, 2018 10:59
-
-
Save andrzj/f6c31b353a59f1f385965e2043f14075 to your computer and use it in GitHub Desktop.
Mongo querys
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
// Filter all URL's that have "/blogs/" in it AND don't have "em-foca". Result limited and pretty | |
db.assets.find({ | |
$and:[ | |
{ url: /\/blogs\// }, | |
{ url: {$not: /em-foca/}}, | |
] | |
}).limit(2).pretty() | |
// Filter and count all URL's that have "/blogs/" in it | |
db.assets.find({url: /^http:\/\/(.*)\/blogs\//}).count() | |
// or | |
db.assets.find({ | |
$and:[ | |
{ url: /\/blogs\// }, | |
{ url: {$not: /em-foca/}}, | |
] | |
}).count() | |
// Filter and print only a specific field | |
db.assets.find({ | |
url: /^https:\/\/(.*)\/blogs\// | |
}).limit(10) | |
.forEach(function(doc, i) { | |
print(doc.url) | |
}) | |
// Filter and iterate through results, counting and printing a specific field if a condition is true. A join-like query | |
db.assets.find({ | |
url: /^https:\/\/(.*)\/blogs\// | |
}).limit(100) | |
.forEach(function(doc, i){ | |
var count = db.comments.find({ | |
asset_id: doc.id | |
}).count(); | |
if (count > 0){ | |
print(doc.id + " - Count:" + count + " - " + doc.url); | |
} | |
}) | |
// Filter, limit the resuls and remove the documents | |
db.assets.find({url: /^https:\/\/(.*)\/blogs\//}).limit(10).forEach(function(doc, i) {db.assets.remove({_id: doc._id}); print(doc.url + " - " + doc.id)}) | |
// Filter and update a document | |
db.assets.find({url: /^http:\/\/(.*)\/blogs\//}).limit(100).forEach(function(doc, i) {doc.url = doc.url.replace(/^http:\/\//,"https://"); db.assets.save(doc); print(doc.url);}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment