Mongodb: No *.system.namespaces - mongodb

I'm trying to repair a database through the console.
It's a replicaset with 3 members, I'm trying in a secondary and I always get the same error. When I list the available collections inside the db it doesn't show any *.namespace collection:
root#web:~# mongo xxx.210.xxx.12:27017/admin -uXXXX -pXXXXXXX
MongoDB shell version: 2.4.5
connecting to: xxx.210.xxx.12:27017/admin
xxxxxSet:SECONDARY> use facebook
switched to db facebook
xxxxxSet:SECONDARY> db.repairDatabase();
{
"ok" : 0,
"errmsg" : "clone failed for facebook with error: namespace query failed facebook.system.namespaces"
}
xxxxxSet:SECONDARY> show collections
xxxnts
xxes
xxxes_beta
system.indexes
system.users
told

show collections hides some collections from view. You can run a direct find on the collection though:
> db.system.namespaces.find();
Which shows:
{ "name" : "test.fs.files" }
{ "name" : "test.system.indexes" }
{ "name" : "test.fs.files.$_id_" }
…
However, if yours is gone then try creating it:
db.createCollection( "facebook.system.namespaces" );

Related

mongo shell ReferenceError: database_name not found

Hello I've been using mongodb with mongo shell and I keep having the same problem.
While the database called Videos exists in MongoDB Compass and I can use it and add data to it, when I try to access it from the mongosh with this command:
Videos.getCollectionNames();
or with this command:
Videos.GBvideos.find();
I keep getting this error:
ReferenceError: Videos is not defined
This happens both with the integrated mongosh (inside the Compass env.) and with the mongosh executable from the installation path.
Any ideas how to get over this error?
I need to use the shell as what I want to do can't be done with the Compass
Compass:
You can do this way in single command:
mongos> db.getSiblingDB("Videos").getCollection("GBVideos").find()
{ "_id" : ObjectId("61eef2439f3fb15d1fef8877"), "test" : 1 }
{ "_id" : ObjectId("61eef24e9f3fb15d1fef8878"), "test" : 1 }
mongos>
Or if you need your way you must assingn to variable:
mongos> var Videos=db.getSiblingDB("Videos")
mongos> Videos.GBVideos.find()
{ "_id" : ObjectId("61eef2439f3fb15d1fef8877"), "test" : 1 }
{ "_id" : ObjectId("61eef24e9f3fb15d1fef8878"), "test" : 1 }
mongos>
use Videos switched to db Videos
is the way to change the database from the mongo shell kudos to R2D2!!

Creating views in mongodb

I'm following this tutorial in creating mongo views
https://www.percona.com/blog/2017/01/13/mongodb-3-4-views/
The issue here is when i run the command
db.createView('employee_names','employee', [{ $project : { _id : 0, "fullname" : {$concat : ["$FirstName", " ", "$LastName"]}}}])
I get an error saying createView is not a function
My mongo version is 3.4. What am i missing .
Views work in MongoDb since version 3.4, so instead of RoboMongo one might need Robo 3T. And of course this will work is shell as well. (Assuming old installations are upgraded to 3.4 as described at https://docs.mongodb.com/master/release-notes/3.4/#upgrade.)
//There are two collection state and City I would Like to create view on them
db.createView(
... "statedistrict", //View Name
... "States", //First Collection Name
... [
... {$lookup:{from:"Districts",localField:"StateId",foreignField:"StateId", as:"trial"}},
... {$project:{"_id":0,"StateId":1,"StateName":1,"trial.DistrictName":1}}
... ]
... )
//After Pressing Enter You Will get below ok message.
{ "ok" : 1 }
//Then pass the below command to see the result/record
db.statedist.find().pretty()
0 means - don't display
1 means - display
From 1st collection I need all to display except _id. So, only _id is kept 0
From 2nd collection I don't need many. So, I have used trial.not_required_field_name : 0 (you can use any name instead of trial)

Mongo db pulling part of the document

I've a document which has another nested document in it represent a changes (logging). Each change document has a timestamp, field, old and new values. Basically as you can see this document would grow a lot, and I really don't want to get all changes but only a recent one.
What I want to do is make a query and get only those changes which falls in between two time stamps. I am not interested in any other information in document, so I dont want to pull it, just recent changes.
{
.......
"adwordsChanges":[
{
"timestamp":NumberLong("1400162491325"),
"field":"syncState",
"old":null,
"new":"OK"
},
{
"timestamp":NumberLong("1400162491325"),
"field":"keywordId",
"old":null,
"new":NumberLong("23918779329")
},
{
"timestamp":NumberLong("1400162491325"),
"field":"adGroupId",
"old":null,
"new":NumberLong("16972286472")
}
]
}
This is what I've tried!
db.keywords.find(
{
$and :[{"_id" : ObjectId("5374c7a7ac58b0d3b5e970fa")}, {"adwordsChanges.field" : "keywordId"}, {"adwordsChanges.timestamp" : {$gte:NumberLong("11111111"), $lte:NumberLong(99999999999999) }}]
})
Running Andrei's query I am getting compilation error:
assert: command failed: {
"errmsg" : "exception: The top-level _id field is the only field currently supported for exclusion",
"code" : 16406,
"ok" : 0
} : aggregate failed
Error: command failed: {
"errmsg" : "exception: The top-level _id field is the only field currently supported for exclusion",
"code" : 16406,
"ok" : 0
} : aggregate failed
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at Function.assert.commandWorked (src/mongo/shell/assert.js:244:5)
at DBCollection.aggregate (src/mongo/shell/collection.js:1149:12)
at (shell):1:13
2014-05-27T15:23:54.945+0100 Error: command failed: {
"errmsg" : "exception: The top-level _id field is the only field currently supported for exclusion",
"code" : 16406,
"ok" : 0
Thanks for any help!
You could use aggregation framework to filter subdocuments, like this:
db.keywords.aggregate({$unwind:"$adwordsChanges"},
{$match:{"adwordsChanges.timestamp" :
{$gte:1400162491325, $lte:23918779329},
"adwordsChanges.field" : "keywordId"}},
{$project:{_id:0,
timestamp:"$adwordsChanges.timestamp",
field:"$adwordsChanges.field",
old:"$adwordsChanges.old",
new:"$adwordsChanges.new"
}});
Explanation of difference between project and group
Initially I thought original document should be returned and group was used to restore original document structure, i.e in this case groupis opposite operation to unwind. After clarification it became clear that only subdocuments were needed, then I replaced group operation with project, so that subdocuments were projected to root level.

enable sharding on all collections existing in a Mongodb database in one shot

I have a huge database , which has some 300 collections , I have to enable sharding for each collections , Is there a command to enable sharding on all collections from mongos ,
lets say my db name is abc
I enabled sharding for db like this
db.runCommand({"enablesharding" : "abc"})
later I need to use
db.runCommand( { shardcollection : "collection name", key : { _id : 1 } } )
foreach collection ,
so Please suggest and easy way of doing it
Thanks in advance for responding to this post
You can get all collection names programmatically.
db.getCollectionNames().forEach(function(coll_name) {
db.runCommand( { shardcollection : coll_name, key : { _id : 1 } } )
})

MongoDB: Can't Remove Documents That Run On Localhost

I can't figure out how to remove documents that I created while testing my app. From within the mongo shell I get this:
> db.carshare.remove();
> show dbs
carshare 0.203125GB
crushFlow 0.203125GB
local (empty)
test (empty)
> db.carshare.remove({});
> show dbs
carshare 0.203125GB
crushFlow 0.203125GB
local (empty)
test (empty)
I'm a beginner and must be missing something very obvious, help?
MongoDB has a hierarchy structure that is DB -> collection -> documents.
So, are you trying to remove a DB, a collection, or a document?
Assuming a DB 'foo', with a collection 'test', with two documents:
> db.test.find();
{ "_id" : ObjectId("4ef54ed6c143a725c52d7ff6"), "name" : "mongo" }
{ "_id" : ObjectId("4ef54eeec143a725c52d7ff7"), "name" : "bob" }
to remove a document:
> db.test.remove({'name':'bob'});
> db.test.find();
{ "_id" : ObjectId("4ef54ed6c143a725c52d7ff6"), "name" : "mongo" }
to remove a collection:
> db.test.drop();
true
> show collections;
system.indexes
to remove a DB:
db.dropDatabase();
{ "dropped" : "foo", "ok" : 1 }
The Mongo documentation is very good:
http://www.mongodb.org/display/DOCS/Overview+-+The+MongoDB+Interactive+Shell#Overview-TheMongoDBInteractiveShell-Deleting
http://www.mongodb.org/display/DOCS/dbshell+Reference
"carshare" and "crushFlow" are the names of databases (which is the output you get from showdb). If you want to see collections to remove documents from type use dbname (test by default) and then type show collections.
The list of collections that appears can have their documents removed by doing db.collection.remove() where collection is the collection name and db is now set to the database you want via the use databasename command.
Good luck and cheers!