return JSON

104 views
Skip to first unread message

Nolan Dubeau

unread,
Feb 4, 2012, 5:34:27 PM2/4/12
to cfmo...@googlegroups.com
Hey folks,

This may be a trivial question, but I haven't been able to figure it out.  Instead of a mongo query result returning as a struct, how would return/get the raw JSON?

Thanks

Nolan

Randy Merrill

unread,
Feb 7, 2012, 10:59:59 AM2/7/12
to cfmo...@googlegroups.com

You can always do a serializejson call on the results. Cfmongodb uses the Java driver, so you never see a json response.

--
You received this message because you are subscribed to the Google Groups "CFMongoDB" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cfmongodb/-/E9sL9iCRWa4J.
To post to this group, send email to cfmo...@googlegroups.com.
To unsubscribe from this group, send email to cfmongodb+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cfmongodb?hl=en.

Marc Esher

unread,
Feb 7, 2012, 11:17:29 AM2/7/12
to cfmo...@googlegroups.com
Yup: serializeJson(results.asArray())

Sent from my miniature iPad

Nelson

unread,
Mar 4, 2013, 1:33:29 PM3/4/13
to cfmo...@googlegroups.com
When I try using the following code: serializeJson(results.asArray()), I get a HUGE packet back that looks like the packet below.  The _id field doesn't appear to serialize very nicely.  Am I doing something wrong?  I was hoping the _id would just be a plain string value.  Any help will be much appreciated.

[{"name":"Test,"version":"17","location":"X92","_id":
{"TimeSecond":1362406888,"New":false,"Machine":-1960317198,"Inc":-560454892,"Time":1362406888000,"CurrentInc":1851309349,"GenMachineId":-219223934,"":
{"TimeSecond":1362421355,"New":true,"Machine":-219223934,"Inc":1851309349,"Time":1362421355000,"CurrentInc":1851309350,"GenMachineId":-219223934,"":

... 6000+ more lines like this ...

{"TimeSecond":1362421356,"New":true,"Machine":-219223934,"Inc":1851315705,"Time":1362421356000,"CurrentInc":1851315706,"GenMachineId":-219223934,"":
{"TimeSecond":1362421356,"New":true,"Machine":-219223934,"Inc":1851315706,"Time":1362421356000,"CurrentInc":1851315707,"GenMachineId":-219223934}]

Thanks,
-Nelson

Marc Esher

unread,
Mar 5, 2013, 9:57:50 AM3/5/13
to cfmo...@googlegroups.com
Yes, that makes sense. the _id field is not a string but a Mongo Java object representing the ID.

I'll need to dig into this further, but I won't have time to do this till at least next week, if not later.

Anyone else have time to figure out some potential solutions to this problem?

The immediate solution is to simply loop over the array and construct a new array which uses the _id.toString() to get the string representation of that object

Marc


To unsubscribe from this group and stop receiving emails from it, send an email to cfmongodb+...@googlegroups.com.

To post to this group, send email to cfmo...@googlegroups.com.

Nelson

unread,
Mar 10, 2013, 8:53:05 AM3/10/13
to cfmo...@googlegroups.com
I've been playing around a little with the core files trying to change the way that the _id property is represented.  Normally, when you get one or more records from mongo, the _id field is an object.  This prevented my ability to simply do a serializeJSON() on the returned array of structs.  So I've made a few changes to have the _id return as the _id.toString() value instead.  I'm new to all of this, so even though this is working very nicely for me, I don't know what impact this will have on others.  Please note that I've only updated the the two following functions.

MongoUtil.cfc
---------------------------------------------------------------------------------------
function toCF(BasicDBObject){
var s = {};
s.putAll(BasicDBObject);
s._id = s._id.toString(); // added this line
return s;
}

DBCollection.cfc
---------------------------------------------------------------------------------------
function update( doc, query, upsert=true, multi=false ){
if ( !structKeyExists(arguments, 'query') ){
arguments.query = {};
}
// update by _id if no query provided
if( structIsEmpty(query) ){
if(isSimpleValue(doc._id)) { // Added this block
doc._id = mongoUtil.newObjectIDFromID(doc._id);
}
query = mongoUtil.newIDCriteriaObject(doc['_id']);
} else {
query = toMongo(query);
}
var dbo = toMongo(doc);
collection.update( query, dbo, upsert, multi );
}


I would be interested in hearing anyone's thoughts on this.

Thanks,
-Nelson

Marc Esher

unread,
Mar 10, 2013, 10:09:17 AM3/10/13
to cfmo...@googlegroups.com
My inclination is to *not* change the _id that is returned. It's been a design goal from the beginning that we try not to stand in the way between mongo and client code.

Instead, I'd prefer to add an "asJson()" method to SearchResult. It'd first call asArray() to get the array of CF structs, and then it'd loop over those results and create a new array that contained the simple value for _id.

Another option would be to pass an additional, optional "transformation function" into the asArray() method, which if present would take in each mongo document, manipulate it as necessary, and then that result would be appended to the array of documents.

The point here is that getting a result as json is a deserialization issue and should be handled as such... it should not get its hooks into the way that the most fundamental stuff in cfmongodb operates.

Marc

Chris Blackwell

unread,
Mar 10, 2013, 10:20:16 AM3/10/13
to cfmo...@googlegroups.com
I'm in agreement Marc. JSON serialization is almost a display issue, not something that should concern the data layer.

And, _id is not only a string identifier, it contains fields like time and machineid, converting the _id to its string representation everywhere would lose the easy access to that data

Cheers, Chris

J Y

unread,
Mar 21, 2013, 9:35:00 PM3/21/13
to cfmo...@googlegroups.com
I had to do the same thing, and I found a way that may work for you.  The DBCursor object will serialize a string to JSON, so I would do the following:

json = "";
result = collection.find().asCursor();
while (result.hasNext()) {
    json = json & result.next().toString();
}

Have not tested this, but you get the idea.  

HTH
Reply all
Reply to author
Forward
0 new messages