mongo-shell

mongodb server-side javascript is client-side actually?

心不动则不痛 提交于 2019-12-08 11:08:15
问题 I have a huge collection of documents and I want to extract some statistics on it. It needs to be executed periodically each 15 minutes. Most of the stats are based on the document size, so I need to fetch the documents and calculate its size. The output of my stats is just a single line with some stats, regarding the size of the documents. (I am not fetching a whole collection, just a subset of it, so I cannot use the collection stats provided by mongodb) What I'd like is to make this

Unordered bulk update records in MongoDB shell

孤街醉人 提交于 2019-12-07 21:01:29
问题 I've got a collection consisting of millions of documents that resemble the following: { _id: ObjectId('...'), value: "0.53" combo: [ { h: 0, v: "0.42" }, { h: 1, v: "1.32" } ] } The problem is that the values are stored as strings and I need to convert them to float/double. I'm trying this and it's working but this'll take days to complete, given the volume of data: db.collection.find({}).forEach(function(obj) { if (typeof(obj.value) === "string") { obj.value = parseFloat(obj.value); db

MongoDB - Get the size of all the documents in a query

只愿长相守 提交于 2019-12-07 16:29:00
问题 Is there's a way to get the size of all the documents that meets a certain query in the MongoDB shell? I'm creating a tool that will use mongodump (see here) with the query option to dump specific data on an external media device. However, I would like to see if all the documents will fit in the external media device before starting the dump. That's why I would like to get the size of all the documents that meets the query. I am aware of the Object.bsonsize method described here, but it seems

Unordered bulk update records in MongoDB shell

空扰寡人 提交于 2019-12-06 07:41:44
I've got a collection consisting of millions of documents that resemble the following: { _id: ObjectId('...'), value: "0.53" combo: [ { h: 0, v: "0.42" }, { h: 1, v: "1.32" } ] } The problem is that the values are stored as strings and I need to convert them to float/double. I'm trying this and it's working but this'll take days to complete, given the volume of data: db.collection.find({}).forEach(function(obj) { if (typeof(obj.value) === "string") { obj.value = parseFloat(obj.value); db.collection.save(obj); } obj.combo.forEach(function(hv){ if (typeof(hv.value) === "string") { hv.value =

MongoDB - Get the size of all the documents in a query

末鹿安然 提交于 2019-12-06 03:24:41
Is there's a way to get the size of all the documents that meets a certain query in the MongoDB shell? I'm creating a tool that will use mongodump (see here ) with the query option to dump specific data on an external media device. However, I would like to see if all the documents will fit in the external media device before starting the dump. That's why I would like to get the size of all the documents that meets the query. I am aware of the Object.bsonsize method described here , but it seems that it only returns the size of one document. Here's the answer that I've found: var cursor = db

can't make basic mongo shell script with authentication

☆樱花仙子☆ 提交于 2019-12-04 18:38:20
问题 I have a really complicated issue that i think i can solve by writing a mongo shell script but i can't even manage to make a simple connection. I have a local mongo database which is requires a username/password that i normally access like this: mongo admin -u <username> -p at which point I enter the password and hooray! i have a shell. but that won't work for my issue. As a test, I created a file called test.js and all it has in it is this: var conn = new Mongo() db = conn.getDB("test"); db

Cannot use commands write mode error, Degrading to compatibility mode

旧街凉风 提交于 2019-12-03 23:55:15
I just play with mongo shell and came across with Cannot use commands write mode, degrading to compatibility mode . I connected to remote mongo server (mongolab) and tried to insert new record to collection by my simple script: // script.js db = connect(host + ":" + port +"/" + dbName); db.auth(username, password); db.test2.insert({ item: "card", qty: 15 }); I run script by mongo script.js and got: MongoDB shell version: 2.6.3 connecting to: test connecting to: my.mongolab.com:port/DBname Cannot use commands write mode, degrading to compatibility mode What is wrong? Additionally when I

How to make comlex query MongoDB with Powershell

≡放荡痞女 提交于 2019-12-03 21:02:51
I need to retrieve data from mongoDB using Powershell. Let's say I have db.orders collection and need to retrieve only orders created during last week and retrieve only specific columns for instance _id, status, createdAt fields. Orders collection schema { "_id": ObjectId("56cf9bab78e9fd46ec557d69"), "status" : "ordered", ... "total": 343, "createdAt": ISODate("2016-01-15T17:29:09.342Z") } I can query it in mongo shell like this db.orders.find({ "createdAt" : { $lt: new Date(), $gte: new Date(new Date().setDate(new Date().getDate()-7)) } }, {_id: 1, status: 1, createdAt: 1 }) But I need to do

Saving the result of a MongoDB query

﹥>﹥吖頭↗ 提交于 2019-12-03 14:59:07
When doing a research in mongo shell I often write quite complex queries and want the result to be stored in other collection. I know the way to do it with .forEach() : db.documents.find(query).forEach(function(d){db.results.insert(d)}) But it's kind of tedious to write that stuff each time. Is there a cleaner way? I'd like the syntax to be something like db.documents.find(query).dumpTo('collectionName') . Here's a solution I'll use: db.results.insert(db.docs.find(...).toArray()) There is still too much noise, though. UPD : There is also an option to rewrite find using aggregation pipeline.

Mongo shell execute query from file and show result

ε祈祈猫儿з 提交于 2019-12-03 14:40:50
问题 How to execute external file using mongo shell and see the result in console? I have external file, like query.js and I would like to execute it and see the result in cmd. Let's say, content of the file is: db.users.find() 回答1: Put this into your query.js file: function get_results (result) { print(tojson(result)); } db.col.find().forEach(get_results) and run: mongo db_name query.js Here's a good explanation why you should do it this way. 回答2: The simplest way I found of running mongodb