How do I get all the names of keys in my documents Posted on May 23, 2017

Question: I heard there are new operators in aggregation to extract keys from my documents. How can I use them to get a list of all key names for all documents in my collection? Answer: There are new expressions available as of 3.4.4 - $objectToArray, $arrayToObject and in 3.5.5 we also get $mergeObjects. Split top level object into array of key value pairs {"$objectToArray":"$$ROOT"} will return an array of {"k":"keyname", "v":<value>} elements.

Read More

How to match documents where all array elements match predicate. Posted on Oct 9, 2016
How do I match all documents where all array elements match some predicate?

Question: I need to match all documents where every element of an array matches some predicate. Can that be done? Answer: Yes, the query to do this is actually quite simple to construct. Remember that when you match an array, MongoDB will “reach inside” the array to compare the predicate to every single array element and return the document if the predicate matches at least one of them. I like to tell MongoDB newbies to think of arrays as a field that can hold many different values at the same time.

Read More

How to Find and Kill Slow Running Queries Posted on Oct 29, 2014

Question: Is there a way that I can prevent slow queries in MongoDB? I’d like to be able to set something on the server that will kill all queries running longer than a certain amount of time. Answer: There are some options available on the client side, for example $maxTimeMS starting in 2.6 release. This gives you a way to inject an option to your queries before they get to the server that tells the server to kill this query if it takes longer than a certain amount of time.

Read More

Further Thoughts on How to Track Versions with MongoDB Posted on Sep 7, 2014

GUEST POST by Paul Done 

In a previous Ask Asya blog post, Asya outlined various approaches for preserving historical versions of records for auditing purposes, whilst allowing current versions of records to be easily inserted and queried. Having found the post to be extremely useful for one of my projects, and following some further investigations of my own, I realised that two of the choices could be refined a little to be more optimal. Upon feeding back my findings, Asya graciously asked me to document them here, so here goes.

Revisit of Choice 2  (Embed Versions in a Single Document)

The presented ‘compare-and-swap’ example code, to generate a new version and update version history, is very effective at ensuring consistency of versions in a thread-safe manner. However, I felt that there was scope to reduce the update latency which will be particularly high when a document has grown, with many previous versions embedded.

For example, if a current document has tens of embedded previous versions, then projecting the whole document back to the client application, updating part of the document copy and then sending the whole document as an update to the database, will be slower than necessary. I prototyped a refactored version of the example code (shown below) which exhibits the same functional behaviour, but avoids projecting the entire document and uses an in-place update to push changes to the database.

Read More

Social Status Feed in MongoDB Posted on Aug 27, 2014

Socialite At MongoDBWorld, my colleague Darren Wood and I gave three back-to-back presentations about an open source project called Socialite which is a reference architecture implementation of a social status feed service. Darren was the one who wrote the bulk of the source code and I installed and ran extensive performance tests in different configurations to determine how the different schema and indexing options scale and to get an understanding of the resources required for various sizes and distributions of workloads.

Read More

Best Versions with MongoDB Posted on May 30, 2014

Question: Recall our previous discussion about ways to recreate older version of a document that ever existed in a particular collection. The goal was to preserve every state for each object, but to respond to queries with the “current” or “latest” version. We had a requirement to be able to have an infrequent audit to review all or some previous versions of the document. Answer: I had suggested at the time that there was a different way to achieve this that I liked better than the discussed methods and I’m going to describe it now.

Read More

How to Merge Shapes with Aggregation Framework Posted on May 24, 2014

Question: Consider two separate shapes of data like this in a single collection: { type: "A", level: 0, color: "red", locale: "USA" } { type: "A", level: 1, color: "blue" } The goal is to present a merged shape to the application with the level n data overridden by level n+1 if level n+1 data exists for type A, starting with n = 0. In other words, the app wants to see this shape:

Read More

How to Track Versions with MongoDB Posted on May 21, 2014

Question: Consider requirement that we have to be able to recreate/query any version of a document that ever existed in a particular collection. So we start out with: { docId: "A", v: 1, color: "red", locale: "USA" } If we need to set color to “blue”, instead of updating the “color” field from “red” to “blue”, we have to create a new version of the document which now has its full “current” state, and preserve somehow the old version of the document.

Read More

What Does FindAndModify Do Posted on May 19, 2014

Question: I saw your answer on SO about the difference between “update” and “findAndModify”, could you explain in more detail what the difference is, and why MongoDB findAndModify is named what it is? Answer: What’s in a name? that which we call a rose By any other name would smell as sweet; - said Juliet1 As it turns out, a lot is in a name. A poorly chosen name can confuse many users, year after year.

Read More

How to Balance Collections Across Your Sharded Cluster Posted on Apr 29, 2014

Question: Is it possible to use “Tag aware sharding” feature without having to use a special shard key? The example in the tutorial makes it look like we would have to change our shard key to have a prefix value that we can define tag ranges on but we’re already sharded. We have many collections in this database and we want to limit each collection to a subset of the shards so we can isolate the busy ones from each other.

Read More