How to exact match entire document?

前端 未结 3 1287
花落未央
花落未央 2021-01-21 01:33

Exact matching subdocuments is easy, but is there a way to exact match entire document in a collection ?

I have a lot of documents with similar data, and I only need e

3条回答
  •  一生所求
    2021-01-21 02:18

    i don't think this is possible outright, but a possible solution is to hash the document.

    when saving, always create a hash of the document:

    var doc = {};
    delete doc.hash; // never include the hash itself in the calculation
    doc.hash = crypto.createHash('sha256').update(JSON.stringify(doc)).digest();
    db.collection.insert(doc);
    

    Then when querying, you can query by hash:

    db.collection.find({
      hash: hash
    })
    

    might be annoying if you frequently do atomic updates on the document.

提交回复
热议问题