mongo-java-driver how to create mongo's user on doesn't exists database yet - mongodb

Can i create the first user on mongodb by mongo-java-driver? i tried but got this error.
com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "Authentication failed." , "code" : 18 , "codeName" : "AuthenticationFailed"}

Related

"show dbs" fails on mongo shell

This is the first time I am trying to connect to this mongo instance, which is setup by my ex-colleague. when I run "show dbs", I have seen such message:
rs0:SECONDARY> show dbs
2022-01-05T18:33:11.282+0000 E QUERY [js] uncaught exception: Error: listDatabases failed:{
"operationTime" : Timestamp(1641407590, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1641407590, 1),
"signature" : {
"hash" : BinData(0,"...="),
"keyId" : NumberLong("...")
}
}
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<#src/mongo/shell/mongo.js:135:19
Mongo.prototype.getDBs#src/mongo/shell/mongo.js:87:12
shellHelper.show#src/mongo/shell/utils.js:906:13
shellHelper#src/mongo/shell/utils.js:790:15
#(shellhelp2):1:1
rs0:SECONDARY>
Any ideas what could be wrong ?
Thanks,
Jack
Following is the screenshot How I got that failure.
You need to execute:
rs.slaveOk()
From the SECONDARY to allow show dbs after ...

MongoDB schema validation failing upon Update but not one insert

I have a Mongodb collection with schema validation.
I executed db.correspondence.validate({full:true}) and received "nInvalidDocuments" : NumberLong(0)
I am able to insert a document but update is failing.
MongoDB Enterprise > db.correspondence.find({"correspondenceIdentifier": "ca4697e2-a40c-11ea-a632-0a0a6b0e0000"}).count()
8
MongoDB Enterprise > db.correspondence.insert({"correspondenceIdentifier": "ca4697e2-a40c-11ea-a632-0a0a6b0e0000",mdmContractIdentifier:'3334444444444444','name':'Vat'});
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.correspondence.find({"correspondenceIdentifier": "ca4697e2-a40c-11ea-a632-0a0a6b0e0000"}).count()
9
MongoDB Enterprise > db.correspondence.find({mdmContractIdentifier:'3334444444444444'}).count()
2
MongoDB Enterprise > db.correspondence.updateOne({"correspondenceIdentifier": "ca4697e2-a40c-11ea-a632-0a0a6b0e0000"},{$set:{mdmContractIdentifier:'3334444444444444'}});
2020-08-05T20:50:28.164-0400 E QUERY [thread1] **WriteError: Document failed validation** :
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"q" : {
"correspondenceIdentifier" : "ca4697e2-a40c-11ea-a632-0a0a6b0e0000"
},
"u" : {
"$set" : {
"mdmContractIdentifier" : "3334444444444444"
}
},
"multi" : false,
"upsert" : false
}
})`
WriteError#src/mongo/shell/bulk_api.js:466:48
Bulk/mergeBatchResults#src/mongo/shell/bulk_api.js:846:49
Bulk/executeBatch#src/mongo/shell/bulk_api.js:910:13
Bulk/this.execute#src/mongo/shell/bulk_api.js:1154:21
DBCollection.prototype.updateOne#src/mongo/shell/crud_api.js:572:17
#(shell):1:1

Aggregations work in standalone but not in sharded cluster

I'm currently trying the aggregations with MongoDB using the json found here : http://media.mongodb.org/zips.json
So, I imported it thousands of time and then I tried this command :
db.CO_villes.aggregate({$group:{_id:"$state",population:{$sum:"$pop"}}})
And I got this error :
2019-04-24T13:49:19.579+0000 E QUERY [js] Error: command failed: {
"ok" : 0,
"errmsg" : "unrecognized field 'mergeByPBRT'",
"code" : 9,
"codeName" : "FailedToParse",
"operationTime" : Timestamp(1556113758, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1556113758, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} : aggregate failed :
I have a sharded cluster with 3 MongoDB instances.
I can face this issue too when I try to get the indexes with "Compass".
I tried to export the data and to remove the id field using the "sed" command (because my Ids were not all with "ObjectID")and to import it but I still face this issue.
I solved my issue by creating a 3.6 cluster instead of a 4.0.6. So I think this is a bug related to the new versions of MongoDB.

Making a replica in MongoDB

I am trying to create a replica in mongodb. I have used the following commands to make a replica. I got the following outputs.
cfg = { _id : "mySet", members : [ { _id : 0, host : "localhost:27017"
} ] }
rs.initiate(cfg)
Output:
{
"ok" : 0,
"errmsg" : "Attempting to initiate a replica set with name mySet, but command line reports 0; rejecting",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig"
}
i get the following output:
017-09-22T14:22:22.093+0530 E QUERY [thread1] ReferenceError: conf is not defined :
#(shell):1:1
When i ran show dbs and rs.config(), i get the following errors:
show dbs
2017-09-22T14:23:56.234+0530 E QUERY [thread1] Error: listDatabases
failed:{
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13 .
Mongo.prototype.getDBs#src/mongo/shell/mongo.js:62:1
shellHelper.show#src/mongo/shell/utils.js:769:19
shellHelper#src/mongo/shell/utils.js:659:15 .
#(shellhelp2):1:1
> rs.config()
2017-09-22T14:24:23.718+0530 E QUERY [thread1] Error: Could not
retrieve replica set config: {
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized"
} :
rs.conf#src/mongo/shell/utils.js:1276:11
#(shell):1:1
delete all replication and oplog.
use local
db.dropDatabase()
restart the mongo
config = {_id: "repl1", members:[
{_id: 0, host: 'localhost:15000'},
{_id: 1, host: '192.168.2.100:15000'}]
}
rs.initiate(config);

MongoDB does Birt support aggregate cursor

When I use Mongodb aggregation with Birt I had the error below:
org.eclipse.datatools.connectivity.oda.OdaException:
Unable to run the Aggregate command operation.
Check that your connected MongoDB server is version 2.2 or later. ;
com.mongodb.CommandResult$CommandFailure: command failed [aggregate]:
{ "serverUsed" : "/xxx.xx.xx.xxx:27017" ,
"errmsg" : "exception: aggregation result exceeds maximum document size (16MB)" ,
"code" : 16389 , "ok" : 0.0 ,
"$gleStats" : { "lastOpTime" : { "$ts" : 0 , "$inc" : 0} ,
"electionId" : { "$oid" : "557cd07784d145278edfba15"}}}
Yes, you can run aggregate MongoDB queries. Check the MongoDB version.