Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7498003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:17:58+00:00 2026-05-29T19:17:58+00:00

Im trying to ‘compare’ all documents between 2 collections, which will return true only

  • 0

Im trying to ‘compare’ all documents between 2 collections, which will return true only and if only all documents inside 2 collections are exactly equal.

I’ve been searching for the methods on the collection, but couldnt find one that can do this.

I experimented something like these in the mongo shell, but not working as i expected :

db.test1 == db.test2

or

db.test1.to_json() == db.test2.to_json()

Please share your thoughts ! Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:18:01+00:00Added an answer on May 29, 2026 at 7:18 pm

    You can try using mongodb eval combined with your custom equals function, something like this.

    Your methods don’t work because in the first case you are comparing object references, which are not the same. In the second case, there is no guarantee that to_json will generate the same string even for the objects that are the same.

    Instead, try something like this:

    var compareCollections = function(){
        db.test1.find().forEach(function(obj1){
            db.test2.find({/*if you know some properties, you can put them here...if don't, leave this empty*/}).forEach(function(obj2){
                var equals = function(o1, o2){
                    // here goes some compare code...modified from the SO link you have in the answer.
                };
    
                if(equals(ob1, obj2)){
                    // Do what you want to do
                }
            });
        });
    };
    
    db.eval(compareCollections);
    

    With db.eval you ensure that code will be executed on the database server side, without fetching collections to the client.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to keep all the presentation stuff in the xhtml on this project and
Trying to read registry keys remotly (from a host on local intranet)... All the
I am trying to create a RegEx expression that will successfully parse the following
Trying to get my 1 table row to automatically clone exactly 24 times and
Trying to get jquery AutoComplete to return and populate it's dropdown. The input and
Trying to get the difference between two DateTime objects. With the below code, I
Trying to get MVC running on Mono 2.4 (which is possible, according to some
Trying to add the ability to delete a Folder using FTP and all subfolders
Trying to get a list of all possible combinations of 2 arrays passed in
Trying to close all forms except for the main menu using FormCollection formsList =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.