Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts

Friday, September 30, 2016

MongoDB "root" Role

To perform functions such as replication administration, the "dbAdminAnyDatabase" role is not enough. The "root" role is required".
db.grantRolesToUser("admin",[{role:"root","db":"admin"}])

Reference: http://stackoverflow.com/questions/23943651/mongodb-admin-user-not-authorized

Wednesday, February 10, 2016

MongoDB "update" Only Updates One Entry by Default

The "update" command only updates the first matching entry by default. To update all entries, there is a third parameter i.e. "options" where you need to set "multi: true" e.g.
db.coll.update({},{$set:{"attr":”value”]}},{multi:true})

Reference: https://docs.mongodb.org/v2.4/reference/method/db.collection.update/

Wednesday, January 13, 2016

MongoDB Security

By default, security and authentication is disabled. Create the administrator account before switching on authentication. Enter the console of the admin DB:
mongo admin

Add the administrator user:
db.createUser({user: "adminUserName",pwd:"adminUserPassword",
   roles: [ { role: "userAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db: "admin" },
{ role: "dbAdminAnyDatabase", db: "admin" },
]
})

Enable authentication by editing the "/etc/mongod.conf" file
Uncomment this line:
auth=true

Restart MongoDB. Enter the console of the admin DB and you'll find that you'll need to authenticate before performing any operations:
db.auth({user: "adminUserName",pwd:"adminUserPassword"})

Now you should be able to add a user for the app database containing your data, and make this user the owner of the app database:
db.createUser({user: "dbUserName",pwd:"dbUserPassword",
   roles: [ { role: "dbOwner", db: "dbName" }]
})
Note: this needs to be done inside the app database

To enable remote login, comment out the "bind_ip" in "/etc/mongod.conf".

Note: Steps tested on MongoDB 2.6.x

Tuesday, January 5, 2016

MongoDB Query Return Multiple Entries for Array Elements

A JSON document stored in MongoDB is tree-based. If it contains multiple sub-elements, they will have to be unwound first before processing.

One way to do that is with the "$unwind" operator in the aggregation framework.

Examples here: https://docs.mongodb.org/v2.4/reference/operator/aggregation/unwind/

Tuesday, August 19, 2014

MongoDB Delete Items from Array

It is possible to delete items from array based on very specific criteria using $pull.

Works for both primitives in an array or complex objects in the array.

Reference: http://docs.mongodb.org/manual/reference/operator/update/pull/

Thursday, August 14, 2014

MongoDB Sorting

Mongo DB command line:
db.collection.find().sort( { score: -1 } )

Mongo DB Java driver:
DBCursor cursor = collection.find();
cursor.sort(new BasicDBObject("score", -1));

Note that "-1" has to be an int, not a String

Reference: http://docs.mongodb.org/manual/reference/operator/meta/orderby/

MongoDB Index

Single field index: http://docs.mongodb.org/manual/core/index-single/

Compound index: http://docs.mongodb.org/manual/core/index-compound/

Monday, July 14, 2014

MongoDB Limiting Fields Returned With Java API

Use the "public DBCursor find(DBObject ref, DBObject keys)" in the DBCollection class:
  • For the "keys", add a key value pair like the following to include a field e.g. "name":
    • key: name
    • value: 1
  • _id is always included by default
  • But if _id is all you want, you should include it in keys, otherwise with an empty "keys" object all fields will be returned

Tuesday, April 22, 2014

MongoDB MapReduce Notes

A few things that stumped me for a while and took me some time to figure out:

  1. Each iteration of the Map step applies only to one document. However, there is a lot of flexibility in terms of what gets emitted in iteration. It can either emit multiple items, or none if certain conditions are not met.
  2. It is also completely up to the user to create the appropriate key in the Map step. The key thing is that all items with the same key (regardless of which document it originated from) all funnels into the same Reduce iteration eventually (it is possible for e.g. 200 values to be reduce to 1 value in multiple steps, with the reduce output from previous steps appearing in the list as one of the items).
  3. The Reduce function will not be called for keys that have single values. This means that the same object structure should be used throughout the process.
  4. There is a "scope" attribute which can be used for global parameters.

Saturday, April 19, 2014

MongoDB Collection Counting

This is done via the method "count()"

db.userAccount.count()

This can be extended to perform conditional counting:

e.g. number of users in New York city

db.userAccount.count({"city":"New York"})

More examples and references: http://docs.mongodb.org/manual/reference/method/db.collection.count/

MongoDB Limit Fields to Return

This is done using the projection feature. Some examples:

To find all entries in userAccount but only have the email returned:

db.userAccount.find({},{"email":"1"});

To find all entries in userAccount from city "New York" and only have the email returned:

db.userAccount.find({"city","New York"},{"email":"1"});

0 can be used to exclude the field

More examples and references: http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/

Thursday, February 6, 2014

MongoDB Commands for Updating Values Inside Document

This page provides a good compilation: http://jonathanhui.com/mongodb-data-update

MongoDB Updating Entry in Array Within Document

This is done via the "$pop" operation but most examples out there operate on simple objects. Here's how it's done for an array that's deeper within the document.

So e.g. if we have a document like this in the "workshop" collection:


{
"_id" : ObjectId("51e216e55cf2ff1231dee123"),
"info" : {
"name" : “Workshop X”,
"city" : “Sacramento”
}
"inventory" : {
"cars" : [
{
"licensePlate" : "XXXX01",
"make" : “Toyota”
},
{
"licensePlate" : "XXXX02",
"make" : “Kia”
},
{
"licensePlate" : "XXXX03",
"make" : “Hyundai”
},
{
"licensePlate" : "XXXX04",
"make" : “Nissan”
},
{
"licensePlate" : "XXXX05",
"make" : “Honda”
}
]
}
}



We could remove the entry at the bottom of the array with the command:

db.workshop.update({"info.name":"Workshop X"} ,{$pop:{"inventory.cars" : 1}});

Note that if you use the dot notation for accessing attributes deeper into the document, you'll need to surround the expression with quotes.

More documentation on $pop here: http://docs.mongodb.org/manual/reference/operator/update/pop/#up._S_pop

Wednesday, November 6, 2013

MongoDB vs MongoLab for Early Stage Live Apps

MongoDB and MongoLab are the 2 main MongoDB hosted service providers who provide their servers within the AWS network and in all regions. For a startup just going live with a new product, volume is initially expected to be low but HA may also be a requirement. Obviously cost is also an issue.

With these in mind the shared instances sound like the way to go, unfortunately with MongoLab this option is not available in all AWS regions. It also comes at a big jump in price from the single server shared plan (i.e. $15 -> $89 for same storage). Bump up the budget to $200 a month and you can get a dedicated instance with much more storage and dedicated RAM. With MongoHQ it gets worse with no cluster / replica-set options available.

So moving on to dedicated instances, MongoLab provides cluster / replica-set options for as little as $200 a month, while MongoHQ doesn't do so until you reach the $1345 a month price plan. For a startup just starting out trying to save costs and not wanting to compromise on HA, seems like MongoLab is the obvious choice purely basing on the packages and price plans.

References:

Articles With Comparison Between DynamoDB and MongoDB (Part 2)