More Index questions

70 views
Skip to first unread message

Dave Brosius

unread,
Jun 22, 2011, 10:59:26 PM6/22/11
to mongo...@googlegroups.com
Thanks for the help.


Two more questions about Indices.


1) I take it the index names are generated and not settable from the
client, correct?


2) The java api 'implies' that an index can be applied to multiple
fields at once, give that

index.get("key")

returns a Map. Is this just an artifact of the java driver
implementation, or 1) can you have multifield indices, or 2) the
potential in the future is to have multi field indices?


Scott Hernandez

unread,
Jun 22, 2011, 11:03:58 PM6/22/11
to mongo...@googlegroups.com
On Wed, Jun 22, 2011 at 10:59 PM, Dave Brosius <dbro...@mebigfatguy.com> wrote:
> Thanks for the help.
>
>
> Two more questions about Indices.
>
>
> 1) I take it the index names are generated and not settable from the client,
> correct?

No, you can choose an index name. If you don't one will be generated for you.

>
> 2) The java api 'implies' that an index can be applied to multiple fields at
> once, give that
>
>        index.get("key")
>
>    returns a Map. Is this just an artifact of the java driver
> implementation, or 1) can you have multifield indices, or 2) the potential
> in the future is to have multi field indices?

1.) you can have compound indexes (with multiple fields and ordering)
2.) your future is here.

http://www.mongodb.org/display/DOCS/Indexes

>
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-dev" group.
> To post to this group, send email to mongo...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-dev...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mongodb-dev?hl=en.
>
>

Dave Brosius

unread,
Jun 22, 2011, 11:10:05 PM6/22/11
to mongo...@googlegroups.com
Ah ensureIndex!

I was looking at

createIndex, and didn't see any way to access the name.


Thanks again.

Dave Brosius

unread,
Jun 22, 2011, 11:21:40 PM6/22/11
to mongo...@googlegroups.com
One more q for the day... sorry for the noise, having difficulty understanding exception message

can only have 1 index plugin / bad index key pattern


When I do:

                    BasicDBObject dbFields = new BasicDBObject();
                    IndexFieldList fieldList = index.getIndexFieldList();

                    for (IndexField field : fieldList) {
                        dbFields.append(field.getFieldName(), field.isAscending() ? "1" : "-1");
                    }

                    System.out.println(dbFields);
                    collection.ensureIndex(dbFields, index.getIndexName());

It prints out

{ "manny" : "-1" , "moe" : "1" , "jack" : "1"}

but get the following exception

Exception in thread "AWT-EventQueue-0" com.mongodb.MongoException: can only have 1 index plugin / bad index key pattern
    at com.mongodb.CommandResult.getException(CommandResult.java:82)
    at com.mongodb.CommandResult.throwOnError(CommandResult.java:116)
    at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:131)
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:153)
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:137)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:255)
    at com.mongodb.DBApiLayer$MyCollection.createIndex(DBApiLayer.java:347)
    at com.mongodb.DBCollection.ensureIndex(DBCollection.java:437)
    at com.mongodb.DBCollection.ensureIndex(DBCollection.java:414)
    at com.mongodb.DBCollection.ensureIndex(DBCollection.java:397)
    at com.mebigfatguy.mongobrowser.actions.ManageIndicesAction.updateIndices(ManageIndicesAction.java:104)



full code seen here

https://github.com/mebigfatguy/mongobrowser/blob/master/src/com/mebigfatguy/mongobrowser/actions/ManageIndicesAction.java

Eliot Horowitz

unread,
Jun 22, 2011, 11:29:12 PM6/22/11
to mongo...@googlegroups.com
You have to use numbers not strings for the key values.

Dave Brosius

unread,
Jun 22, 2011, 11:38:42 PM6/22/11
to mongo...@googlegroups.com
thank you - that was it.
Reply all
Reply to author
Forward
0 new messages