MongoDB query: character count for each record

296 views
Skip to first unread message

C Hill

unread,
Oct 14, 2014, 2:57:28 PM10/14/14
to mongod...@googlegroups.com
Hello,

I'm a total nubie with MongoDB and need to find the largest record based on character count.

What is the query which will display the ObjectId of the record and it's character count from the largest record to the smallest record?

_id = 55678      char count = 15
_id = 6578        char count = 11
_id = 1234        char count = 10

Thanks,

CHill
===============================

XML Record format appears below:

"_id" ObjectId("1234");
"fruit" : {
"fruit1" : "apple",
"fruit2" : "orange",
"fruit3" : "pear",
}


"_id" ObjectId("55678");
"fruit" : {
"fruit1" : "blueberry",
"fruit2" : "orange",
"fruit3" : "pear",
}

"_id" ObjectId("6578");
"fruit" : {
"fruit1" : "apple",
"fruit2" : "orange",
"fruit3" : "banana",
}





Will Berkeley

unread,
Oct 14, 2014, 3:39:07 PM10/14/14
to mongod...@googlegroups.com
What is the string whose characters you're going to count? The stringified version of your entire record? Just of the values? The records you included aren't in XML format; they're more or less in JSON format. MongoDB doesn't have a function to compute the character count and return the largest for you - you'd have to compute the character count, store it on the documents, and then use a query to return the documents sorted by character count.

-Will

C Hill

unread,
Oct 14, 2014, 3:54:43 PM10/14/14
to mongod...@googlegroups.com
Will - You are right.  The records were originally XML then converted to JSON when uploaded to MongoDB.

Yes, I would like the "stringified" version of each record in MongoDB (flattened record?). Then a function to compute the character count finally printing the record IDs each with a count value.  I guess this is just not possible in MongoDB. 

The BSON size of the record - which is easy to compute in MongoDB, doesn't quite do it.  I don't think there is a correlation between record character count and record BSON size, right? Thanks for your help.

Will Berkeley

unread,
Oct 14, 2014, 4:04:28 PM10/14/14
to mongod...@googlegroups.com
Why do you want the character count of the whole record? What meaning does it have? Maybe you just want some subset of the fields, and you can include the character count of those fields as another field? Regardless, there isn't a function in MongoDB to compute the character length of a record because it's not very meaningful. BSON size is obviously correlated with "number of characters" but for documents with similar shapes and field values, the BSON size might order them differently than the character count of some stringification of the record.

-Will

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/4a2f9c31-a736-43c9-ad80-778671019481%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages