0

This is my first project using mongodb, I doing a file upload website and everyhting is working smooth, now what I need is to get of names of file which are duplicate by checking there checksum. All the info is already on the database here is an example of how im saving the info

{
    "_id" : ObjectId("58754c4257edd7263c678fb9"),
    "checksum" : "8473b6ba72ca7aa3041d3f473fb38e12",
    "destination" : "upload/",
    "fieldname" : "file[0]",
    "mimetype" : "application/x-zip-compressed",
    "size" : 4531435,
    "path" : "upload/1484082242254-file-1.5.zip",
    "filename" : "1484082242254-file-1.5.zip",
    "originalname" : "file-1.5.zip"
}

now what i need is a mongodb query which will return the filename and/or path of all the duplicate files with the same checksum

thank you for your time and help

Elyas74
  • 548
  • 1
  • 5
  • 18
mandaman2k
  • 33
  • 6

1 Answers1

0

If you connected to DB, you code is something like this, a simple query:

db
.collection_name
.find({
    $and: [{
            checksum: new_checksum
        },
        {
            filename: new_name
        }
    ]
})
.lean()
.exec(function(err, dup_files) {
    // dup_files is all files with this checksum
})
Elyas74
  • 548
  • 1
  • 5
  • 18
  • thanks for the reply, just one question, what does the lean() do? and what do you mean with new_checksum and new_name? thanks – mandaman2k Jan 10 '17 at 23:12
  • find return an mongoose object for you that have many methods for use, but when you need just data, you can `lean` it, and it converted to raw json of just data. – Elyas74 Jan 10 '17 at 23:14
  • thanks didnt know about `lean`, it will be very helpful in the future for my project, what do you mean with new_checksum and new_name? what should i put in there? – mandaman2k Jan 10 '17 at 23:26
  • `new_checksum` and `new_name` are detail of file you want to add to DB, and you want to check that file. – Elyas74 Jan 10 '17 at 23:29
  • ok ok, so `new_checksum` is where i would put the checksum i would like to search for? cause what i need is for it to search the whole collections to find the duplicates – mandaman2k Jan 10 '17 at 23:39
  • Yes, you should put in in `new_checksum` in string, something like this "4473b6ba72ca7aa3041d3f473fb38e22" or if it's in a variable just put that var instead of `new_checksum`. And one more thing I write `and` in query,you should check you need and use `or` or `and`! – Elyas74 Jan 10 '17 at 23:43