0

I am using Couchbase Lite database for project which is schemaless as I know, and I am very happy with that one because it solves my issues, but it raise me one question related with primary key contraints in NoSQL (Document Database).

As we all know that all Schema Database will be represented in tables, and these tables may or may not have primary/forgien key. For example lets suppose I have a table called Student which has the the primary key as usn(University seat number), along with other attributes, firstname, lastname, address, contactnumber, etc etc.

usn | firstname | lastname | address | contactnumber

2BA11CS409 | abc | mnq | Bangalore | 1234567890

2BA11CS410 | xyz | PQR | Mumbai | 1234567809

Here the table will through an error saying that violation of primary key constraints (cannot added duplicate key) if I tried to add 2BS11CS409 value once again.

But what is the case in Document Database, how it will identify unique value within document,

docID:123456789zxcv

{
usn : 2BA11CS409,
firstname : abc,
.......
....... etc
}

I know each document has one unique Id whose key is indexed for searching in database, but what I created another document with same values as above,

docID:zxcv123456789
{
usn : 2BA11CS409,
firstname : abc,
last
....... etc
}

when I try to access one database with usn, it has to return me just one document, but it will return me two document may be identical or different.

I need to know primary/unique key kind of concept in document database, which is exist in Relational database. OR you can redirect me to some articles

Thank You.

Community
  • 1
  • 1
Nasir
  • 1,617
  • 2
  • 19
  • 34
  • Thanks @Nils Ziehn, but what happens in case if I wanna to reterive a document based on usn and two documents with same usn is present in database, will it show two documents or throws any error. any example would be appreciated – Nasir Jul 23 '15 at 07:32

1 Answers1

0

Well, schema or no schema unique key constraints always use an index, the advantage for most RDBs is that they provide this service for you, in case of couchbase you need to do it yourself. Basically you have a second collection, which represents the index. Each time you want to insert in you standard collection, you just check the index first whether a document with the usn you want to insert exists, if it doesn't you insert the document into the normal collection. Then you insert a document into the index with docId=usn and if you want to you can insert a reference to the docID of the document you inserted into the normal collection.

Nils Ziehn
  • 4,118
  • 6
  • 26
  • 40
  • After some deep research I came to some point. I can use `docID` itself to store the `USN`, instead of some junk/random value in it. Before Inserting the value in database I can check whether this document with `docID : 2BA11CS409` is present, if its _true_ I can prevent them to insert same value. – Nasir Sep 11 '15 at 06:48