Comparing documents

2,072 views
Skip to first unread message

WDB

unread,
Jul 26, 2012, 1:16:59 PM7/26/12
to mongod...@googlegroups.com
In order to determine if two documents in a collection are identical (except for key data), I can query for the first document, parse the results into another query, and then execute that. Wondering if there is a better, slicker, idea. Thanks.

Jenna

unread,
Jul 26, 2012, 2:38:09 PM7/26/12
to mongodb-user
What is the goal of document comparison, and how many comparisons will
you need to make at any given time? In addition, how are your querying
for the first document- using a single field, such as the _id?

I'm not sure if there's a better way, but additional information about
your data model and queries may help to inspire creative solutions.

On Jul 26, 1:16 pm, WDB <walter.breidenst...@thomsonreuters.com>
wrote:

WDB

unread,
Jul 26, 2012, 3:20:19 PM7/26/12
to mongod...@googlegroups.com
In the future, I could see the need to determine if ANY other document in this collection is like THIS one (determined by a key). But really, right now, I have two values for a document key ( _id ), and I was hoping to use the database to determine if the documents are identical (in fields, and field-values).

I realize that I could extract both, put the data in files, and then do a file compare. But since this is such an easy task in a relational database, I thought it might be do-able in Mongo. Maybe I should not only take my Relational hat off, but put it in the closet.

Taras Voynarovsky

unread,
Jul 27, 2012, 7:03:45 AM7/27/12
to mongod...@googlegroups.com
Comparing 2 documents would be quite problematic, because the data is sorted, etc.. The task itself is quite unique. Do you control the process of data updates? Maybe it would be more efficient to add a version or hash sum to all documents in this collections and compare those by querying both and comparing on client?

четверг, 26 июля 2012 г., 22:20:19 UTC+3 пользователь WDB написал:

Sam Millman

unread,
Jul 27, 2012, 7:12:18 AM7/27/12
to mongod...@googlegroups.com
I would personally hash the document and then compare, version numbers are only really useful for trying to make mongo transactional.

With a hash field you could just get one doc and then all docs that equal that has but not that _id.

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Albert Kam

unread,
Jul 27, 2012, 10:09:17 AM7/27/12
to mongod...@googlegroups.com
Hi WDB,

In the past, i have quite a different situation where i need to
compare collections instead of comparing documents,
and the solution that i put into use is to compare using javascript.

These are the related links :
http://stackoverflow.com/questions/1068834/object-comparison-in-javascript
http://stackoverflow.com/questions/9240636/how-to-compare-2-mongodb-collections

Anyway, i am also interested in the easy sql way that you mentioned if
you dont mind to share, since it could be helpful to find the mongodb
equivalents for it ..

Hope that helps !

--
Do not pursue the past. Do not lose yourself in the future.
The past no longer is. The future has not yet come.
Looking deeply at life as it is in the very here and now,
the practitioner dwells in stability and freedom.
(Thich Nhat Hanh)

Nilesh Gupta

unread,
Jun 16, 2016, 4:35:49 PM6/16/16
to mongodb-user, moonbla...@gmail.com
Hi Albert,

I am looking to compare two collections to find our if there is any change in documents for further processing , will it work with the way suggeted by you 
Reply all
Reply to author
Forward
0 new messages