mongo-shell

can't make basic mongo shell script with authentication

时间秒杀一切 提交于 2019-12-03 12:13:44
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.cust.find(); I then run the script from the command line like so: mongo test.js at which point i get

extract subarray value in mongodb

眉间皱痕 提交于 2019-12-03 05:19:52
问题 MongoDB noob here... I have a collection as follows... > db.students.find({_id:22},{scores:[{type:'exam'}]}).pretty() { "_id" : 22, "scores" : [ { "type" : "exam", "score" : 75.04996547553947 }, { "type" : "quiz", "score" : 10.23046475899236 }, { "type" : "homework", "score" : 96.72520512117761 }, { "type" : "homework", "score" : 6.488940333376703 } ] } how do I display only the quiz score via mongo shell? 回答1: You have some syntax in your original example which probably isn't doing what you

Mongo shell execute query from file and show result

人走茶凉 提交于 2019-12-03 04:28:59
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() alecxe 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. The simplest way I found of running mongodb queries from a file and seeing the output in the console is this: query.js : use my_db; db.my_collection

Iterate over all Mongo database

只谈情不闲聊 提交于 2019-12-02 20:54:15
I'm relatively new to MongoDB and I've not been able to find a solution for what I'm looking for. I would like to iterate over all mongo databases and run some command on each collection of each database. I can run the following command to get all db names: db.runCommand( { listDatabases: 1 } ).databases.forEach(function (db) { print ("db=" + db.name); }); But how do I "switch" database within forEach loop so I can run query against each database? I want to use something like use db.name within loop but that's not working. You can use db.getSiblingDB() to switch between databases and db

extract subarray value in mongodb

时间秒杀一切 提交于 2019-12-02 18:36:49
MongoDB noob here... I have a collection as follows... > db.students.find({_id:22},{scores:[{type:'exam'}]}).pretty() { "_id" : 22, "scores" : [ { "type" : "exam", "score" : 75.04996547553947 }, { "type" : "quiz", "score" : 10.23046475899236 }, { "type" : "homework", "score" : 96.72520512117761 }, { "type" : "homework", "score" : 6.488940333376703 } ] } how do I display only the quiz score via mongo shell? You have some syntax in your original example which probably isn't doing what you expect .. that is, it looks like your intent was to only match scores for a specific type ('exam' in your

RoboMongo can't use array filters?

流过昼夜 提交于 2019-12-01 20:44:10
问题 Following on from "How to update collection and increment hours for ISO date". It seem that RoboMongo can't execute a query with an array filter: This same query if find when executed through the Mongo shell though: > db.collection.update({ ... "results.score": { ... "$exists": true ... } ... }, { ... "$mul": { ... "results.$[result].score": 10 ... } ... }, { ... "arrayFilters": [{ ... "result.score": { ... "$exists": true ... } ... }], ... "multi": true ... }) WriteResult({ "nMatched" : 1,

MongoDB select all where field value in a query list

青春壹個敷衍的年華 提交于 2019-12-01 11:31:06
How to achieve below SQL in MongoShell? Select TableA.* from TableA where TableA.FieldB in (select TableB.FieldValue from TableB) Mongo doc gives some example of db.inventory.find( { qty: { $in: [ 5, 15 ] } } ) I want that array be dynamically from another query. Is it possible? Extending my question I have a collection of bot names bots collection { "_id" : ObjectId("53266697c294991f57c36e42"), "name" : "teoma" } I have a collection of user traffic, in that traffic collection, I have a field useragent userTraffic Collection { "_id" : ObjectId("5325ee6efb91c0161cbe7b2c"), "hosttype" : "http",

Read image file into a MongoDB document's binary field via mongo shell script

纵然是瞬间 提交于 2019-12-01 09:43:10
I would like to read an image file into a MongoDB document's binary field from mongo shell. I can do this in Java with MongoDB Java driver. However, I would like to be able to do with a mongo script from mongo shell. Is this possible? For example, I would like to do this: D:\mongo\bin> mongo --shell myscript.js where myscript.js is as follow: conn = new Mongo(); db = conn.getDB("mydb"); db.mycoll.remove(); db.mycoll.insert( { name : "LCD monitor", thumbnail : Binary(0, **cat("D:\\images\\lcdmonitor.jpg")**) } ); As is, the use of cat() method gives "InternalError: buffer too small (anon):1",

MongoDB select all where field value in a query list

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 09:34:36
问题 How to achieve below SQL in MongoShell? Select TableA.* from TableA where TableA.FieldB in (select TableB.FieldValue from TableB) Mongo doc gives some example of db.inventory.find( { qty: { $in: [ 5, 15 ] } } ) I want that array be dynamically from another query. Is it possible? Extending my question I have a collection of bot names bots collection { "_id" : ObjectId("53266697c294991f57c36e42"), "name" : "teoma" } I have a collection of user traffic, in that traffic collection, I have a field

Connect to a specific database by default in mongodb

大憨熊 提交于 2019-12-01 07:35:24
I am running a mongodb on a linux box. So every time I connect to it from the console (typing mongo ) I get something like this: MongoDB shell version: 2.4.9 connecting to: test And then I am doing use myDatabase (where myDatabase is 99% is the same). So basically I always do some unneeded type of work. Is there a way to configure mongo, so that it will connect to myDatabase by default? Surprised that I don't find a duplicate of this. Okay, now we have content. From the command line, just do this: $ mongo myDatabase This actually is covered in the documentation , albeit down the page somewhat.