0

How to add a primary key to a specific column in MongoDB?

How to avoid duplicate records in MongoDB?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1

create a unique index on the field https://docs.mongodb.com/manual/core/index-unique/

njLT
  • 464
  • 6
  • 21
  • okay , thank you @njLT How about removing the Duplicate records – Priyanka Baviskar Jan 02 '18 at 06:27
  • if you have the unique index from the start - you won't have duplicate records. take a look at https://stackoverflow.com/questions/30187688/mongo-3-duplicates-on-unique-index-dropdups – njLT Jan 02 '18 at 06:31
  • see over here https://stackoverflow.com/questions/14184099/fastest-way-to-remove-duplicate-documents-in-mongodb – Avij Jan 02 '18 at 06:46
  • I want to make combination of 2 columns as unique key @Avij – Priyanka Baviskar Jan 02 '18 at 10:51
  • If you are looking at the solution mentioned in link before...try adding another field here in group : _id: { name: "$name",anotherField:'$anotherField'}, @PriyankaBaviskar – Avij Jan 02 '18 at 11:03
  • create a unique index on the field https://docs.mongodb.com/manual/core/index-unique/ this link worked perfect for me – Priyanka Baviskar Jan 02 '18 at 12:14
1

_id is the default reserved id for the mongodb object and that's an unique id. refer that link it will help you to get the idea of unique id http://www.mongodb.org/display/DOCS/BSON

sainanky
  • 507
  • 4
  • 13