Why MongoDB saves Hash value in different format?

64 views Asked by At

I am learning IPFS and trying to save IPFS file info in MongoDB. After uploading, I get one hash value

ex: {fileHash: CID(QmbCaWPi9tSqcnykvBUMaH2M1d5PiVPLEfPwhJksSSeMak)}

And in MongoDB, I see something like this:

"fileHash": {
    "code": 112,
    "version": 0,
        "multihash": {
            "code": 18,
             "size": 32,
             "digest": "vxaISoMOXTpFy9rwnYilyk7pa6TlhkFDCZgcjGhXfm0=",
             "bytes": "EiC/FohKgw5dOkXL2vCdiKXKTulrpOWGQUMJmByMaFd+bQ=="
       },
    "bytes": "EiC/FohKgw5dOkXL2vCdiKXKTulrpOWGQUMJmByMaFd+bQ=="
}

Now I want to update this document based on hash value but don't understand what value I compare in match.

 db.collection("test").update(filter, set);

I was trying like this:

db.collection("test").update({fileHash:"CID(QmbCaWPi9tSqcnykvBUMaH2M1d5PiVPLEfPwhJksSSeMak)"}, set);

But it doesn't work

2

There are 2 answers

0
YuTing On

I think CID() is like a function.

When you use CID("QmbCaWPi9tSqcnykvBUMaH2M1d5PiVPLEfPwhJksSSeMak"), it come up with a JSON.

{
    "code": 112,
    "version": 0,
        "multihash": {
            "code": 18,
             "size": 32,
             "digest": "vxaISoMOXTpFy9rwnYilyk7pa6TlhkFDCZgcjGhXfm0=",
             "bytes": "EiC/FohKgw5dOkXL2vCdiKXKTulrpOWGQUMJmByMaFd+bQ=="
       },
    "bytes": "EiC/FohKgw5dOkXL2vCdiKXKTulrpOWGQUMJmByMaFd+bQ=="
}

So you should try something like

let fileHash = CID("QmbCaWPi9tSqcnykvBUMaH2M1d5PiVPLEfPwhJksSSeMak");
db.collection("test").update({fileHash: fileHash }, set);
0
ctnava On

Take that CID and use the method toString() on it.