Mongo Case Insensitive index (collation) not getting results - mongodb

I'm trying to use the CaseInsensitive Index using Mongoid 5. I even went down to the Mongo console to run the experiments but it won't pull the results at all:
> db.charges.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "paymentsapi_development.charges"
}
]
> db.charges.createIndex({'details.email':1},{name:'emails_index',collation:{locale:'en',strength:1}})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.charges.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "paymentsapi_development.charges"
},
{
"v" : 1,
"key" : {
"details.email" : 1
},
"name" : "emails_index",
"ns" : "paymentsapi_development.charges",
"collation" : {
"locale" : "en",
"strength" : 1
}
}
]
> db.charges.find({'details.email':'lolo#lolo.com'}).count()
1
> db.charges.find({'details.email':'LoLo#LoLo.cOm'}).collation({locale:'en',strength:1}).count()
0
>
I'm basing in the Collation (https://docs.mongodb.com/manual/reference/collation/) and the Case Insensitive Index (https://docs.mongodb.com/manual/core/index-case-insensitive/) .
I already checked the db version, the db.adminCommand( { setFeatureCompatibilityVersion: <version> } ) based on https://docs.mongodb.com/manual/reference/command/setFeatureCompatibilityVersion/ and all further things possible.
Any help is very appreciated.

Related

mongodb - indexes disappear after few minutes

Have tested this on MongoDB 3.4 and 3.6:
Create one or more indexes in a collection
rs1:PRIMARY> db.coll.createIndex({checkinDate:1}, {background:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1,
"operationTime" : Timestamp(1518162276, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1518162276, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Now list the indexes.
rs1:PRIMARY> db.coll.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "cico.coll"
},
{
"v" : 2,
"key" : {
"checkinDate" : 1
},
"name" : "checkinDate_1",
"ns" : "cico.coll",
"background" : 1
}
]
Wait for some time (few mins)
List the indexes again:
rs1:PRIMARY> db.coll.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "cico.coll"
}
]
I have no clue why these indexes created are getting deleted? Any help appreciated.
Index option background is of type boolean, try:
db.coll.createIndex (
{ checkinDate:1 }, { background: true }
)
this is actually a non-issue. there was a program, running in the background, which was dropping all the indexes periodically (for a completely different reason).

MongoDB TTL sparse index?

Can you TTL a sparse field? If so, should you declare the TTL index sparse? Like so?
db.eventlog.createIndex( { "lastModifiedDate": 1 }, { expireAfterSeconds: 3600 , sparse:"true"} )
> use foo
switched to db foo
> db.foo.createIndex({date: 1}, {expireAfterSeconds: 5, sparse: true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.foo.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "foo.foo"
},
{
"v" : 1,
"key" : {
"date" : 1
},
"name" : "date_1",
"ns" : "foo.foo",
"expireAfterSeconds" : 5,
"sparse" : true
}
]
> db.foo.insert({date: new Date()})
> db.foo.find()
{ "_id" : ObjectId("5841aeb650b5412e92ebbb9b"), "date" : ISODate("2016-12-02T17:26:14.617Z") }
> db.foo.find()
>
It appears that this worked fine. Note that according to the documentation , the TTL operation fires every 60 seconds or so, so the expireAfterSeconds: 5 may take longer.

When renaming a MongoDB collection, are the indices intact?

When performing this operation, will indices remain intact?
db.collection('my-collection').rename('new-collection-name', {dropTarget:true});
Using the mongo cli, it's easy to test:
$ mongo
> db.bob.ensureIndex({ name: 1 })
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.bob.renameCollection('robert', { dropTarget: true })
{ "ok" : 1 }
> db.robert.getIndices()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.robert"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test.robert"
}
]
So, yes, it looks like the indices do remain intact.

Issue When I set shard key

I have a collection and I set shard it.
I have errors while I add a shard key!!!
mongos> sh.shardCollection('IBSng.connection_log', {login_time:1})
But I was shown this error:
Even I set compound shard with logout_time field with login_time, But the result shown to me this error.
{
"proposedKey" : {
"login_time" : 1
},
"curIndexes" : [
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"user_id" : 1
},
"name" : "user_id_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"ras_id" : 1
},
"name" : "ras_id_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"retry_count" : 1
},
"name" : "retry_count_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"credit_used" : 1
},
"name" : "credit_used_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"details.mac" : 1
},
"name" : "details.mac_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"username" : 1
},
"name" : "username_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"type_details.in_bytes" : 1,
"type_details.out_bytes" : 1
},
"name" : "type_details.in_bytes_1_type_details.out_bytes_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"details.kill_reason" : 1
},
"name" : "details.kill_reason_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"details.terminate_cause" : 1
},
"name" : "details.terminate_cause_1",
"ns" : "IBSng.connection_log"
},
{
"v" : 1,
"key" : {
"login_time" : -1,
"logout_time" : -1
},
"name" : "login_time_-1_logout_time_-1",
"ns" : "IBSng.connection_log",
"sparse" : false
}
],
"ok" : 0,
"errmsg" : "please create an index that starts with the shard key before sharding."
}
I Waiting for your answers.
Add an index for {login_time:1}, this is different to that compound one you have done.

can't shard a collection on mongodb

I have a db ("mydb") on mongo that contains 2 collections (c1 and c2). c1 is already hash sharded. I want to shard a second collection the same way. I get the following error :
use mydb
sh.shardCollection("mydb.c2", {"LOG_DATE": "hashed"})
{
"proposedKey" : {
"LOG_DATE" : "hashed"
},
"curIndexes" : [
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "mydb.c1",
"name" : "_id_"
}
],
"ok" : 0,
"errmsg" : "please create an index that starts with the shard key before sharding."
So I did
db.c2.ensureIndex({LOG_DATE: 1})
sh.shardCollection("mydb.c2", {"LOG_DATE": "hashed"})
Same error but it shows the new index.
"proposedKey" : {
"LOG_DATE" : "hashed"
},
"curIndexes" : [
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "mydb.c2",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"LOG_DATE" : 1
},
"ns" : "mydb.c2",
"name" : "LOG_DATE_1"
}
],
"ok" : 0,
"errmsg" : "please create an index that starts with the shard key before sharding."
Just to be sure, I run :
db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "mydb.c1", "name" : "_id_" }
{ "v" : 1, "key" : { "timestamp" : "hashed" }, "ns" : "mydb.c1", "name" : "timestamp_hashed" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns": "mydb.c2", "name" : "_id_" }
{ "v" : 1, "key" : { "LOG_DATE" : 1 }, "ns" : "mydb.c2", "name" : "LOG_DATE_1" }
I try again the same commands on admin and it fails with the same error.
Then I tried on admin without "hashed" and it worked.
db.runCommand({shardCollection: "mydb.c2", key: {"LOG_DATE": 1}})
Problem : now my collection is sharded on something that is not hashed and I can't change it (error : "already sharded")
What was wrong with what I did ?
How can I fix this ?
Thanks in advance
Thomas
The problem initially was that you did not have a hashed index what you proposed to use for sharding this is the error message is about. After the first error message, when you created an index which is
{
"v" : 1,
"key" : {
"LOG_DATE" : 1
},
"ns" : "mydb.c2",
"name" : "LOG_DATE_1"
}
You still just have an ordinary index which is not a hashed one. If you would do this :
db.c2.ensureIndex({LOG_DATE: "hashed"})
Instead of this :
db.c2.ensureIndex({LOG_DATE: 1})
Than would be a hashed index. As you can see in the output of the db.system.indexes.find() on the other collection you have a hashed index for the timestamp i assume this is the shard key for that collection.
So if you have no data in the c2 collection:
db.c2.drop()
db.createCollection('c2')
db.c2.ensureIndex({LOG_DATE: "hashed"})
sh.shardCollection("mydb.c2", {"LOG_DATE": "hashed"})
This will work properly.