How to create unique index through create index.

166 views
Skip to first unread message

Raman Goyal

unread,
Nov 20, 2015, 11:26:43 AM11/20/15
to mongodb-user
I am trying to create a unique index via create index method. But following command is not providing a unique index.

collQueries.createIndex(Document("name" -> 1, "unique" -> true))

And neither this command.

collQueries.createIndex(Document("name" -> 1, "unique" -> true), IndexOptions()) 

 Can anyone please help on how to create unique index using Mongo-Scala Driver.


Asya Kamsky

unread,
Nov 21, 2015, 1:48:36 AM11/21/15
to mongodb-user
Create index takes two arguments. The first specifies the index, the second specifies options.

I'm guessing you want something like:

collection.createIndex(Document("name" -> 1), Document("unique" -> false))

If that's not in the ballpark, you'll have to look at the docs for exact syntax you need:


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/f7b692b2-b656-482d-a9c4-c223757a9306%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Asya Kamsky
Lead Product Manager
MongoDB
Download MongoDB - mongodb.org/downloads
Free MongoDB Monitoring - cloud.mongodb.com
Free Online Education - university.mongodb.com
Get Involved - mongodb.org/community
We're Hiring! - https://www.mongodb.com/careers

Raman Goyal

unread,
Nov 22, 2015, 2:12:34 PM11/22/15
to mongodb-user
I looked into it and finally figure out that this is the way to create unique index.

collQueries.createIndex(Document("name" -> 1), IndexOptions().unique(true))

But to my surprise this is not creating any unique index in the database. Can you please look into that?


Wan Bachtiar

unread,
Nov 24, 2015, 8:01:40 PM11/24/15
to mongodb-user

Hi Raman,

The MongoDB Scala Driver uses the Observable model. All Observables returned from the API are cold, meaning that no I/O happens until they are subscribe to.

For example, you can use toFuture():scala.concurrent.Future[Seq[T]]) which automatically subscribes to the Observable and uses the collect:org.mongodb.scala.Observable[Seq[T]]) method to aggregate the results.

collection.createIndex(Document("user" -> 1), IndexOptions().unique(true)).toFuture()


Also have a look at some helper methods defined in Helpers.scala to get and print results. For example, using the results() blocks until the Observable is completed and returns the collected results

// Returns: { "v" : 1, "unique" : true, "key" : { "name" : 1 }, "name" : "name_1", "ns" : "mydb.test" }
collection.createIndex(Document("name" -> 1), IndexOptions().unique(true)).results()

Check out the Quick Tour Primer for more information on the Observable API.


Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages