Skip to content

Instantly share code, notes, and snippets.

@andrzj
Last active June 8, 2018 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrzj/f6c31b353a59f1f385965e2043f14075 to your computer and use it in GitHub Desktop.
Save andrzj/f6c31b353a59f1f385965e2043f14075 to your computer and use it in GitHub Desktop.
Mongo querys
// 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