mongo-shell

How to list all collections in the mongo shell?

岁酱吖の 提交于 2019-11-27 16:33:46
In the MongoDB shell, how do I list all collections for the current database that I'm using? AdaTheDev You can do... JS (shell): db.getCollectionNames() node.js: db.listCollections() non-JS (shell only): show collections The reason I call that non-JS is because: $ mongo prodmongo/app --eval "show collections" MongoDB shell version: 3.2.10 connecting to: prodmongo/app 2016-10-26T19:34:34.886-0400 E QUERY [thread1] SyntaxError: missing ; before statement @(shell eval):1:5 $ mongo prodmongo/app --eval "db.getCollectionNames()" MongoDB shell version: 3.2.10 connecting to: prodmongo/app [ "Profiles

Printing Mongo query output to a file while in the mongo shell

♀尐吖头ヾ 提交于 2019-11-27 11:04:34
问题 2 days old with Mongo and I have a SQL background so bear with me. As with mysql, it is very convenient to be in the MySQL command line and output the results of a query to a file on the machine. I am trying to understand how I can do the same with Mongo, while being in the shell I can easily get the output of a query I want by being outside of the shell and executing the following command: mongo localhost:27017/dbname --eval "printjson(db.collectionName.findOne())" >> sample.json The above

cannot use the part (…) to traverse the element

落花浮王杯 提交于 2019-11-27 09:47:19
Running mongod 3.6 and attempting to use this example . Here is the sample data: > db.students2.find().pretty() { "_id" : 1, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 8 }, { "grade" : 85, "mean" : 90, "std" : 6 }, { "grade" : 85, "mean" : 85, "std" : 8 } ] } { "_id" : 2, "grades" : [ { "grade" : 90, "mean" : 75, "std" : 8 }, { "grade" : 87, "mean" : 90, "std" : 5 }, { "grade" : 85, "mean" : 85, "std" : 6 } ] } I am attempting to use the all positional operator as specified in the example: > db.students2.update({}, { $inc: { "grades.$[].std" : -2 } }, {multi: true}) WriteResult({

How to aggregate by floor in MongoDB

六眼飞鱼酱① 提交于 2019-11-27 08:05:35
问题 MongoDB Aggregation Framework doesn't have floor function. It only has simple arithmetic operators. So, how to compose floor function using them? 回答1: According to definition floor(number) = number - (number % step) we can compose our aggregation formula: {$subtract: ["$number", {$mod: ["$number", <step>]}]} where step is order of magnitude. First, it computes remainder with $mod and then it computes difference with $subtract . So, grouping Users by age floored to whole numbers will be db

Loop through all Mongo collections and execute query

孤街醉人 提交于 2019-11-26 23:14:39
问题 First of, I'm quite new to mongodb. Here's my question I've not been able to find a solution to. Let's say I have 3 different collections. mongos> show collections collectionA collectionB collectionC I want to create a script that iterates over all collections ind this database and find the last inserted timestamp in each of these collections. Here's what works inside mongos. var last_element = db.collectionA.find().sort({_id:-1}).limit(1); printjson(last_element.next()._id.getTimestamp());

How to replace substring in mongodb document

馋奶兔 提交于 2019-11-26 17:33:54
I have a lot of mongodb documents in a collection of the form: { .... "URL":"www.abc.com/helloWorldt/..." ..... } I want to replace helloWorldt with helloWorld to get: { .... "URL":"www.abc.com/helloWorld/..." ..... } How can I achieve this for all documents in my collection? Naveed db.media.find({mediaContainer:"ContainerS3"}).forEach(function(e,i) { e.url=e.url.replace("//a.n.com","//b.n.com"); db.media.save(e); }); Louisa Currently, you can't use the value of a field to update it. So you'll have to iterate through the documents and update each document using a function. There's an example

How to replace substring in mongodb document

守給你的承諾、 提交于 2019-11-26 04:46:08
问题 I have a lot of mongodb documents in a collection of the form: { .... \"URL\":\"www.abc.com/helloWorldt/...\" ..... } I want to replace helloWorldt with helloWorld to get: { .... \"URL\":\"www.abc.com/helloWorld/...\" ..... } How can I achieve this for all documents in my collection? 回答1: db.media.find({mediaContainer:"ContainerS3"}).forEach(function(e,i) { e.url=e.url.replace("//a.n.com","//b.n.com"); db.media.save(e); }); 回答2: Starting Mongo 4.2 , db.collection.update() can accept an