[JAVA-1383] Use an non-existing field to create index, it should not successfull. Created: 18/Aug/14  Updated: 19/Aug/14  Resolved: 19/Aug/14

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 2.12.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Dong [X] Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDB 2.6.3



 Description   

When I create index, use a non-existing filed to create, it should throw exception, but the index has been created.



 Comments   
Comment by Dong [X] [ 19/Aug/14 ]

Thank you for your explain. I got it.

Thanks,
Rambo

Comment by Ross Lawley [ 19/Aug/14 ]

Hi Rambo,

Just to let you know this really is a SERVER, mailing list or stackoverflow type question, the JAVA project is specifically for driver based bugs or issues.

However, to answer in short this is a feature of MongoDB. In the relational database world you can also create an index without having any stored data in a table, however, you do describe a schema upfront. One of MongoDB's unique selling points is the fact it supports dynamic schemas through its document model providing more flexibility. As such in MongoDB you can create an index at any time for any field and there may or may be matching documents at the time of creation. In fact planning and creating indexes up front is often preferred by DBA's as there can be some operational impact on creating new indexes on large collections.

I should also highlight that if your data model is changeable on a document by document basis or you are indexing a rarely stored field then a sparse index would be more space efficient.

I hope that clarifies why you can create an index on fields that don't already exist.

Comment by Dong [X] [ 19/Aug/14 ]

Hi Jeff,

I used java code as below:

DB db = mongo.getDB("Test");
DBCollection dbCollection = db.getCollection("testCollection");
DBObject indexKey = (DBObject) JSON.parse( "

{names:1}

" );
dbCollection.createIndex(indexKey);

The "testCollection" is a new created collection, have not create document. So the field "names" is non-existing for "testCollection" documents.

Use non-existing field to create index, it should not success.

Thanks,
Rambo

Comment by Jeffrey Yemin [ 18/Aug/14 ]

Please post a code snippet, explaining what you mean by a non-existing field.

Thanks,
Jeff

Generated at Thu Feb 08 08:54:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.