javascript - Prevent Duplicate Documents in MongoDB? - Stack Overflow

I'm trying to figure out the best way to prevent duplicate documents from being saved in MongoDB.R

I'm trying to figure out the best way to prevent duplicate documents from being saved in MongoDB.

Right now my form takes the user_url from the user. The logic is:

  • Check if the user_url is valid. (dns.lookup)

  • If user_url is new, save it to the database and return url_ID.

  • If user_url is old, just return the url_ID.

I think my 2 options are:

var findOneURL = function(user_url, done) {
    URL.findOne({
        orig_url: user_url
    }, (err, data) => {
        if (err) {
            done(err);
        }
        done(null, data);
    })
}

or

var findEditThenSave = function(user_url, done) {
    URL.findById(user_url, (err, data) => {
        if (err) {
            done(err);
        }

        data.save((err, data) => {
            if (err) {
                done(err);
            }
            done(null, data);
        });
    })
};

It's not working terribly well at the moment but this is the live version: /

EDIT2: I think I got it working properly now. Here's my logic:

Saving to database: dns.lookup -> findByURL -> (If it doesn't exist) -> countURLs -> findAndUpdateURL -> Return what was saved to database.

OR -> (If it exists) -> Return the record.

Retrieving from database: findByID

I'm trying to figure out the best way to prevent duplicate documents from being saved in MongoDB.

Right now my form takes the user_url from the user. The logic is:

  • Check if the user_url is valid. (dns.lookup)

  • If user_url is new, save it to the database and return url_ID.

  • If user_url is old, just return the url_ID.

I think my 2 options are:

var findOneURL = function(user_url, done) {
    URL.findOne({
        orig_url: user_url
    }, (err, data) => {
        if (err) {
            done(err);
        }
        done(null, data);
    })
}

or

var findEditThenSave = function(user_url, done) {
    URL.findById(user_url, (err, data) => {
        if (err) {
            done(err);
        }

        data.save((err, data) => {
            if (err) {
                done(err);
            }
            done(null, data);
        });
    })
};

It's not working terribly well at the moment but this is the live version: https://kindly-fisherman.glitch.me/

EDIT2: I think I got it working properly now. Here's my logic:

Saving to database: dns.lookup -> findByURL -> (If it doesn't exist) -> countURLs -> findAndUpdateURL -> Return what was saved to database.

OR -> (If it exists) -> Return the record.

Retrieving from database: findByID

Share edited Jan 3, 2019 at 4:01 Adam Weiler asked Jan 2, 2019 at 2:38 Adam WeilerAdam Weiler 5716 silver badges15 bronze badges 2
  • 1 Since you are using mongoose you can use unique: true property in the model. mongoosejs./docs/2.7.x/docs/schematypes.html – Shams Nahid Commented Jan 2, 2019 at 4:04
  • I would suggest to use unique index on single field. docs.mongodb./manual/core/index-unique. This will throw an error while inserting duplicate row. Based on the error code you can easily find out this. – Amaranadh Meda Commented Jan 2, 2019 at 5:56
Add a ment  | 

2 Answers 2

Reset to default 4

The best choice is findOneAndUpdate query with upsert and returnNewDocument options

db.collection.findOneAndUpdate({ orig_url: user_url }, { $set: { orig_url: user_url }}, { upsert: true, returnNewDocument: true })

In mongoose

URL.findOneAndUpdate({orig_url: user_url }, { $set: { orig_url: user_url }}, { upsert: true, new: true }, (err, data) => {
    if (err) {
        done(err);
    }
    // data will contain your document
    done(null, data);
});

upsert option specifies whether to insert document if not found, new (returnNewDocument in mongo's console) - whether to return old or updated document - if false (default is false) you will have null for inserted documents.

Instead of using

 db.insert() 

you should use

db.update() 

and specify

$upsert: true

[here]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744947292a4602698.html

相关推荐

  • javascript - Prevent Duplicate Documents in MongoDB? - Stack Overflow

    I'm trying to figure out the best way to prevent duplicate documents from being saved in MongoDB.R

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信