Not able to create sharded collection - database

I have a mongodb Cluster with one config node(replication),one query node , and one shard node(with replication).
I tried to create sharded collection but it is giving me error:
command that i ran :
use exampleDB
sh.enableSharding("exampleDB")
Output:
{
"ok" : 1,
"operationTime" : Timestamp(1611723057, 4),
"$clusterTime" : {
"clusterTime" : Timestamp(1611723057, 4),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Now creating a collection :
db.exampleCollection.ensureIndex( { _id : "hashed" } )
Output :
{
"raw" : {
"shardSet/clb1pidnmd3.ccsm.in:27017" : {
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"commitQuorum" : "votingMembers",
"ok" : 1
}
},
"ok" : 1,
"operationTime" : Timestamp(1611723093, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1611723093, 3),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Now sharding collection
sh.shardCollection( "exampleDB.exampleCollection", { "_id" : "hashed" } )
Output :
{
"ok" : 0,
"errmsg" : "Shard shard0000 not found",
"code" : 70,
"codeName" : "ShardNotFound",
"operationTime" : Timestamp(1611723116, 5),
"$clusterTime" : {
"clusterTime" : Timestamp(1611723116, 5),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Not able to figure out what to do now

Before these steps, you need to have first added the shard servers through below command in Mongo.
sh.addShard("shard0000/IP:Port")

Related

Insert numbers into mongo-db collection

I am currently learning Mongo DB and trying to insert numbers into my "numbers" collection (in mongo command shell).
//This works :
db.numbers.insertMany([{"number":1},{"number":2}]);
//This doesn't
db.numbers.insertMany([1,2,3,4,5,6]);
(1) Does that mean that number is not a valid document or I am missing a very basic concept ?
(2) Why Mongo-db is not assigning Object-ID to numbers automatically in this case ?
//actual output from Mongoshell version 4.2.6 command line
> db.numbers.insertMany([{"number":1},{"number":2}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5f79fa89d04cd9e2b3acbf03"),
ObjectId("5f79fa89d04cd9e2b3acbf04")
]
}
> db.numbers.find();
{ "_id" : ObjectId("5f79fa89d04cd9e2b3acbf03"), "number" : 1 }
{ "_id" : ObjectId("5f79fa89d04cd9e2b3acbf04"), "number" : 2 }
> db.numbers.insertOne({number:[1,2,3,4,5,6]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5f79fa9ed04cd9e2b3acbf05")
}
> db.numbers.find();
{ "_id" : ObjectId("5f79fa89d04cd9e2b3acbf03"), "number" : 1 }
{ "_id" : ObjectId("5f79fa89d04cd9e2b3acbf04"), "number" : 2 }
{ "_id" : ObjectId("5f79fa9ed04cd9e2b3acbf05"), "number" : [ 1, 2, 3, 4, 5, 6 ] }
>

MongoDB 3.6 - $inc two fields at the same time with findAndModify or update

I have MongoDB 3.6 installed with the repo as configured in the installation guide. I have CentOS 7.
Now, I have this document:
{
"_id" : ObjectId("5a561e324f5b1e2c0066b568"),
"owners" : [
{
"owner" : "Juan",
"from" : "1990",
"to" : "1995"
},
{
"owner" : "Diego",
"from" : "1995",
"to" : "1997"
},
{
"owner" : "Alonso",
"from" : "1997",
"to" : "1998"
}
]
}
I want to $inc the from and to at the same time with only one update. Can it be possible?
I am with:
db.guitar.findAndModify({
query: {},
update: {
$set: {
$inc:{"owners.$[element].from":1, "owners.$[element].to":2}
}
},
arrayFilters: [{"element.owner":{$eq:"Juan"}}],
new: true
})
with error:
2018-02-16T15:44:58.138+0100 E QUERY [thread1] Error: findAndModifyFailed failed: {
"ok" : 0,
"errmsg" : "The array filter for identifier 'element' was not used in the update { $set: { $inc: { owners.$[element].from: 1.0, owners.$[element].to: 2.0 } } }",
"code" : 9,
"codeName" : "FailedToParse"
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
DBCollection.prototype.findAndModify#src/mongo/shell/collection.js:724:1
#(shell):1:1
If I use update:
> db.guitar.update(
... {"owners.owner":"Juan"},
... {$set: {$inc:{"owners.$.from": 3333, "owners.$.to":4444}}},
... {"multi":true}
... )
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 52,
"errmsg" : "The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage."
}
})
It does not work neither.
So, is it possible?
The correct update query would be:
db.guitar.findAndModify({
query: {},
update: {
$inc:{"owners.$[element].from":1, "owners.$[element].to":2}
},
arrayFilters: [{"element.owner":{$eq:"Juan"}}],
new: true
})
notice you don't need a set only a $inc, however $inc only works with numerical values so you will receive the following error:
E QUERY [thread1] Error: findAndModifyFailed failed: {
"ok" : 0,
"errmsg" : "Cannot apply $inc to a value of non-numeric type. {_id: ObjectId('5a561e324f5b1e2c0066b568')} has the field 'from' of non-numeric type string",
"code" : 14,
"codeName" : "TypeMismatch"
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
In this case convert the value to be numerical and try it again.

Mongodb replication (secondary server)

I set up a master-slave replication of MongoDB by using 2 servers. The problem is I always going to assign rs.slaveOk() in slave server after inserting data in master. I want to automatically synced(no need to rs.slaveOk()) in secondary! What configurations should I need to change? Thanks !
This is my rs.conf()for master-slave replication!
> rs2:PRIMARY> rs.conf() { "_id" : "rs2", "version" : 3,
> "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 0,
> "host" : "192.168.56.104:27017", "arbiterOnly" : false,
> "buildIndexes" : true, "hidden" : false, "priority" : 1,
> "tags" : {
> }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "192.168.56.106:27017", "arbiterOnly" :
> false, "buildIndexes" : true, "hidden" : false, "priority" :
> 0, "tags" : {
> }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true,
> "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10,
> "electionTimeoutMillis" : 10000, "getLastErrorModes" : {
> }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("5a1e37704f3b7025eccaa874") } }
You can create a file /etc/mongorc.js and add rs.slaveOk() there. The file is being evaluated on each shell startup.
You can have a look here for more clarification.
Or, another way is to start mongo with the following command:
>mongo --port 27012 --eval "rs.slaveOk()" --shell
slaveOk() is only valid for that console session that it was executed in, so you would need to pass in a script or stay connected to the console with the --shell arguments.

How to retrieve distinct keys inside an object in MongoDB

I have this in MongoDB:
{ "_id" : ObjectId("58fb35531eb5df245d5d434f"), "name" : "d1.html", "indexation" : { "Citroen" : 1, "color" : 1, "Marca" : 1, "rojo" : 1 } }
{ "_id" : ObjectId("58fb35531eb5df245d5d4350"), "name" : "d2.html", "indexation" : { "blancos" : 1, "ocasión" : 1, "Madrid" : 1, "Coches" : 1, "rojo" : 1, "coches" : 1 } }
{ "_id" : ObjectId("58fb35531eb5df245d5d4351"), "name" : "d3.html", "indexation" : { "rojos" : 1, "Ocasión" : 1, "marcas" : 1, "Madrid" : 1, "blancas" : 1, "autos" : 1, "de" : 1 } }
You can see an image containing the above:
And I would like to get the distinct keys inside the object "indexation" in each document.
The result I woul like to get is: ["Citroen", "color", "Marca", "rojo", "blancos", "ocasión", "Madrid", "Coches", "coches", "rojos", "Ocasión", "marcas", "blancas" "autos", "de"].
I'm trying with distinct ("indexation") but I get the whole indexation...
Could you explain to me what do I have to do to get what I want, please?
You can use new $objectToArrray in 3.4.4 version to convert all key & value pair into document arrays followed by $unwind & $group with $addToSet to get distinct keys
db.collection.aggregate([{$project: {indexation: {$objectToArray: "$indexation"}}}, {$unwind:"$indexation"}, {$group:{_id:null, keys:{$addToSet:"$indexation.k"}}}])
For lower version you've to update indexation to look like below and and use
db.collection.distinct("indexation.k")
{ "_id" : ObjectId("58fb35531eb5df245d5d434f"), "name" : "d1.html", "indexation" : [{ "k" : "Citroen", "v" : 1 }, { "k" : "Marca", "v" : 1 }]}

unable to use $pullAll

I have a collection which contains the documents as shown below
{
"_id" : ObjectId("56d92901f9d573cc1c1fb8bb"),
"busEntryExitInformation" : {
"dateTime" : ISODate("2016-03-04T06:19:45.914+0000"),
"busEntryExitEvent" : [
{
"plateNumber" : "ADFN3R2",
"direction" : "EXIT",
"routeNumber" : NumberInt(929),
"driverID" : "DId5",
"driverName" : "john",
"_id" : ObjectId("56d92901f9d573cc1c1fb8c0")
},
{
"plateNumber" : "ADFN3R4",
"direction" : "EXIT",
"routeNumber" : NumberInt(652),
"driverID" : "DId2",
"driverName" : "jack",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bf")
},
{
"plateNumber" : "ADFN3R8",
"direction" : "EXIT",
"routeNumber" : NumberInt(500),
"driverID" : "DId5",
"driverName" : "john",
"_id" : ObjectId("56d92901f9d573cc1c1fb8be")
},
{
"plateNumber" : "ADFN3R7",
"direction" : "ENTRY",
"routeNumber" : NumberInt(500),
"driverID" : "DId3",
"driverName" : "mack",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bd")
},
{
"plateNumber" : "ADFN3R2",
"direction" : "EXIT",
"routeNumber" : NumberInt(652),
"driverID" : "DId2",
"driverName" : "sandesh",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bc")
}
],
"cameraIntrinsics" : {
"cameraFocalLength" : NumberInt(35),
"cameraAngle" : NumberInt(20),
"imageWidth" : "1920",
"imageHeight" : "1080",
"frameRate" : NumberInt(25)
},
"cameraExtrinsics" : {
"cameraId" : NumberInt(1),
"cameraName" : "Under Route-090 NorthboundBridge",
"cameraDirection" : "Towards Northbound Lanes",
"cameraLatitude" : 1.350228,
"cameraLongitude" : 103.984889,
"cameraHeight" : NumberInt(30)
}
},
"__v" : NumberInt(0)
}
}
where busEntryExitEvent is a array i am trying to remove the array elements where direction is exit across all the documents in the collection
result should contain all the documents with direction entry
{
"_id" : ObjectId("56d92901f9d573cc1c1fb8bb"),
"busEntryExitInformation" : {
"dateTime" : ISODate("2016-03-04T06:19:45.914+0000"),
"busEntryExitEvent" : [
{
"plateNumber" : "ADFN3R7",
"direction" : "ENTRY",
"routeNumber" : NumberInt(500),
"driverID" : "DId3",
"driverName" : "mack",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bd")
}
],
"cameraIntrinsics" : {
"cameraFocalLength" : NumberInt(35),
"cameraAngle" : NumberInt(20),
"imageWidth" : "1920",
"imageHeight" : "1080",
"frameRate" : NumberInt(25)
},
"cameraExtrinsics" : {
"cameraId" : NumberInt(1),
"cameraName" : "Under Route-090 NorthboundBridge",
"cameraDirection" : "Towards Northbound Lanes",
"cameraLatitude" : 1.350228,
"cameraLongitude" : 103.984889,
"cameraHeight" : NumberInt(30)
}
},
"__v" : NumberInt(0)
}
}
i am trying to do like this
db.busEntryExitDoc.update(
{ $pull: { busEntryExitEvent: { "direction" : "EXIT"} } },
{ multi: false }
)
this has to be done to all the documents there is no where condition here how to do it please help
The MongoDB update() methods expects three arguments: db.collection.update(query, update, options). If you want to perform the update on all documents, just use an empty object as query:
db.busEntryExitDoc.update({},
{ $pull: { 'busEntryExitInformation.busEntryExitEvent' : {'direction' : "EXIT"} } },
{ multi: true }
);
Also, note the use of the dot notation to access the array field: you have to point the $pull operator to an array otherwise the operation fails. Finally, use { multi: true } as options since you want to update all the documents in your collection.
You are doing it wrong the first argument to .update() is the "query" argument which should be an empty document here.
db.busEntryExitDoc.update(
{},
{ $pull: { "busEntryExitInformation.busEntryExitEvent": { "direction": "EXIT" } } },
{ multi: true }
)
The same thing apply to the .updateMany() method
db.busEntryExitDoc.update(
{},
{ $pull: { "busEntryExitInformation.busEntryExitEvent": { "direction": "EXIT" } } },
)

Resources