Getting MongoDB error - SyntaxError: Unexpected token? - mongodb

I am a Mongo database. There is a collection called _migrations. I am trying to insert couple of documents into the collection. But I am getting the following error SyntaxError: Unexpected token ?. My code is below.
db._migrations.insert([
{
"_id" : "1_create-organization-collection"
},
{
"_id" : "2_create-dataFilter-collection"
},
{
"_id" : "3_create-application-collection"
},
{
"_id" : "4_migrate-datafilters-to-mongo"
},
{
"_id" : "5_Add-Salesforce-DataFilters"
},
{
"_id" : "6_biq-repository-data-fiter"
}]);
What am I doing wrong?

Can you please trying removing underscore("") in your collection name? I tried with underscore but it did not work for me, but when I tried without underscore("") same data got inserted. So, please try without underscore in your collection name.

_id field value can be inserted after type case with ObjectId. it will work like below -
db._migrations.insert([
{ "_id" : ObjectId("1_create-organization-collection") },
{ "_id" : ObjectId("2_create-dataFilter-collection") }
]);

Please refer below links for more details about the issue:
Is there a convention to name collection in MongoDB?
Mongo client can't access collections prefixed with an underscore
Mongo shell does not support collection name starting with underscore("_")

Related

mongoDB TypeError when trying to search in database

I have a big database with many documents in one collection. When i use findOne(). It gives me this
> db.sem.findOne()
{
"_id" : ObjectId("56619c6852a9c022d077400b"),
"name" : "MANAN VIJAY",
"reg_no" : "11011",
"dept" : "B.Tech",
"subjects" : {
},
"sem" : "5"
}
now when I try
> db.cse_sem_5.find()(
... {"name": "MANAN VIJAY"}
... )
2015-12-08T14:24:21.052+0530 E QUERY TypeError: object is not a function
at (shell):1:20
Why am I getting this error? And how can I resolve it?
This did not happen when I tried something with a smaller database..
Your find() query looks wrong, try like this:
db.cse_sem_5.find({"name": "MANAN VIJAY"})

Mongodb field name as numbers - how to use it in updates?

Because of a weird bug in my code, I have a collection with field names which are stricly numbers (ex: 34344,54675,34356).
Now I try to move these fields values to another fields (ex: name,email,etc) but when I run the update command:
db.collection.find({"id_field" : 1996}).forEach(function (elem) {db.collection.update({_id: elem._id},{$set: {name: elem.36536}});});
All I get is an error:
SyntaxError: Unexpected number
How should I handle it? I already tried with elem[36536] instead elem.36536 but without success.
if I got your problem right, this code might help you:
db.collection.find({"id_field" : 1996}).forEach(function (elem)
{
var value = elem['36536'];
db.collection.update({_id: elem._id},{$set: {name: value}});
});
If your document is like this
{
"_id" : 1,
"elem" : {
"123" : "barno",
"456" : "foo#gmail.com"
}
}
you can $rename the key document.
db.d.update({"_id" : 1},{$rename:{'elem.123':'elem.name','elem.456':'elem.email'}})
Result:
{
"_id" : 1,
"elem" : {
"name" : "barno",
"email" : "foo#gmail.com"
}
}

MongoDB writing a correct query

I am new to MongoDB and Im trying to write a query to extract data from a collection I created but I am stuck in trying to do so. Any help would be greatly appreciated.
I want to get all the fields except _id for all movies that are NOT based on things
How could I do this in a MongoDB query? I've read and looked up many things trying to learn about Mongo but so far I haven't gotten anything. Thank you very much in advance.
This is my collection:
{ "_id" : ObjectId("5363738hhhe2282828282w"), "coll" : "PageMaster" }
{ "_id" : ObjectId("0000222211223333sssswq2"), "coll" : "Honey1", "Jink" : { "head" : "Jink1"} }
{ "_id" : ObjectId("5hjkwwowwj7373365252wwww"), "coll" : "Rodger", "things" : { "head": "Honey"} }
Just check for the records where the key BOOK does not exist. Those are the records that we want. And exclude the _id field in the projection parameter.
db.MOVI.find({"BOOK":{$exists:false}},{"_id":0})
EDIT:
For using printjson to print the cursor contents:
var myCursor = db.MOVI.find({"BOOK":{$exists:false}},{"_id":0});
myCursor.forEach(function(doc)
{
printjson(doc);
})

Updating MongoDB object field

I have searched and tried many answers but I can't seem to get this working. I have a user in MongoDB (db.users...) and I'm trying to update a field titled 'role' in one particular document. If I perform a :
db.users.find();
I get:
{ "_id" : "fDx2g34G8vxsDu3vf", "createdAt" : ISODate("2014-06-03T16:31:47.382Z"),
"emails" : [ { "address" : "andrewmlarking#gmail.com", "verified" : false } ],
"profile" : { "name" : "Admin", "role" : "user", "division" : "0", "enrolled" : "false" },
"services" : { "password" : { "srp" : { "identity" "dEad06c_5mjzsprUzgRyh6tB66OiaybdLzbnxFzO1xh",
"salt" : "zqWsh etc etc
Which is fine, if I then perform a:
db.users.update({_id:"2xnoy3jqcHCaFp7Br"}, { role:"admin"});
I get a:
assert failed : need an object Error: Printing Stack Trace
at printStackTrace (src/mongo/shell/utils.js:37:15)
at doassert (src/mongo/shell/assert.js:6:5)
at assert (src/mongo/shell/assert.js:14:5)
at DBCollection.update (src/mongo/shell/collection.js:220:5)
at (shell):1:10
Any ideas?
Thanks.
First of all if you are trying to update the role field of the profile subdocument your syntax is off. It should be:
db.users.update({_id:"2xnoy3jqcHCaFp7Br"}, {$set: { "profile.role":"admin"}})
Otherwise you will just delete all the other fields of the document.
Regarding the error message, that normally only occurs when you run update with a single argument (the query) without the fields to update. Are you sure you are calling it as documented in your question?
Regardless I would highly advise you to review MongoDB documentation for updates:
http://docs.mongodb.org/manual/tutorial/modify-documents/
Maybe you should try adding ObjectId to the query.
Also, as pointed by #John Petrone you should use $set
I usually do
db.users.update({_id:ObjectId("2xnoy3jqcHCaFp7Br")}, {$set: { "profile.role":"admin"});

mongo db update subdocument - subdocument not in array

I have a document like this,
{
"S" : {
"500209" : {
"total_income" : 38982,
"interest_income" : 1714,
"reported_eps" : 158.76,
"year" : 201303
}
},
"_id" : "pl"
}
I am trying to update this document like this,
{
"S" : {
"500209" : {
"total_income" : 38982,
"interest_income" : 1714,
"reported_eps" : 158.76,
"year" : 201303,
"yield": 1001, <== inserted a new attribute
}
},
"_id" : "pl"
}
I have tried this,
db.my_collection.update({_id: 'pl'},{$set: {'S.500209.yield': 1}})
But I couldn't make it. And I searched in stack overflow and google but I couldn't find out.
I got so many answers but most of them keeping sub-docuemnts in array.
Pleas help me to solve my issue, and please tell me why most of them keeping subdocuments in array.
Number key might cause the problem. Update your field name.
db.my_collection.update({_id: 'pl'},{$set: {'S.a500209.yield': 1}})
EDIT
Upgrade mongo version. It works fine with 2.4.