mongo-shell

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

女生的网名这么多〃 提交于 2019-12-19 10:42:38
问题 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\

Connect to a specific database by default in mongodb

风格不统一 提交于 2019-12-19 09:06:52
问题 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? 回答1: Surprised that I don't find a duplicate of this. Okay, now we have content. From the command line,

query in mongo Shell gives SyntaxError: missing : after property

╄→гoц情女王★ 提交于 2019-12-19 03:12:21
问题 db.movieDetails.find( { year: 2013, imdb.rating: Pg-13, award.wins: 0 }, { title: 1, _id: 0 } ).pretty(); The mongo shell returns this error 2016-08-13T09:08:00.648+0200 E QUERY [thread1] SyntaxError: missing : after property id @(shell):1:60 Why? Thank you in advance! 回答1: If your query includes inner documents, then use quotes for them. Also, use quotes for querying String values db.movieDetails.find( { year: 2013, "imdb.rating": "Pg-13", "award.wins": 0 }, { title: 1, _id: 0 } ).pretty();

Update in forEach on mongodb shell

左心房为你撑大大i 提交于 2019-12-17 23:16:59
问题 I have got a collection aTable with 2 records: { "title" : "record 1", "fields" : [ { "_id" : 1, "items" : [ 1 ] }, { "_id" : 2, "items" : [ 2,3,4 ] }, { "_id" : 3, "items" : [ 5 ] } ] }, { "title" : "record 2", "fields" : [ { "_id" : 4, "items" : [ 7,8,9,10 ] }, { "_id" : 5, "items" : [ ] }, { "_id" : 6, "items" : [ 11,12 ] } ] } I want to update fields aTable.fields.items from items : [ 11,12 ] to items : [ {item: 11, key: 0}, {item:12, key: 0} ] I browse fields with forEach but I can't

cannot use the part (…) to traverse the element

£可爱£侵袭症+ 提交于 2019-12-17 10:06:03
问题 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

Change document structure in mongodb with the mongo shell [duplicate]

▼魔方 西西 提交于 2019-12-11 12:43:20
问题 This question already has an answer here : Reshape all the documents in the collection (1 answer) Closed 3 years ago . I have a document structure in mongodb and i want to change it for all my documents without using aggregate function like this response but by creating a specific function, and have a result from that : { "_id" : ObjectId("String"), "content" : { "href" : "String", "text" : "String", "code" : "String " } } to that : { "_id" : ObjectId("String"), "href" : "String", "text" :

Order of key values in mongoDb [duplicate]

微笑、不失礼 提交于 2019-12-11 02:45:07
问题 This question already has answers here : MongoDB field order and document position change after update (7 answers) Closed 5 years ago . mongo code : db.temperature.insert({"x":3,"y":4}); db.temperature.find(); OUTPUT { "_id" : ObjectId("52b418fb132c1f3236831447"), "y" : 4, "x" : 3 } Please help me to understand why in my case(above.) The find method is showing Y value first and x value later even when while inserting the order is different. Appreciate any help. 回答1: Quoting https:/

How to add an extra field in a sub document in MongoDB?

大兔子大兔子 提交于 2019-12-10 22:45:09
问题 I've just started working with MongoDB. And I have a document like this: { "_id": "12345" "body": "Here is the body" "comments":[ { "name": "Person 1" "comm": "My comment"}, { "name": "Person 2" "comm": "Comment 2"} ] "author":"Author 1" } And I want to change this document to : { "_id": "12345" "body": "Here is the body" "comments":[ { "name": "Person 1" "comm": "My comment" "checks_": 1 }, { "name": "Person 2" "comm": "Comment 2" "checks_": 4 } ] "author": "Author 1" } I've tried: db.coll

MongoDB convert string type to float type

大憨熊 提交于 2019-12-10 17:26:42
问题 Following the suggestions over here MongoDB: How to change the type of a field? I tried to update my collection to change the type of field and its value. Here is the update query db.MyCollection.find({"ProjectID" : 44, "Cost": {$exists: true}}).forEach(function(doc){ if(doc.Cost.length > 0){ var newCost = doc.Cost.replace(/,/g, '').replace(/\$/g, ''); doc.Cost = parseFloat(newCost).toFixed(2); db.MyCollection.save(doc); } // End of If Condition }) // End of foreach upon completion of the

Saving the result of a MongoDB query

倖福魔咒の 提交于 2019-12-09 12:35:27
问题 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') . 回答1: Here's a solution I'll use: db.results.insert(db.docs.find(...).toArray()) There