Unexpected token error when using MongoDB - mongodb

Attempting to setup / populate a basic mongo db using the shell, I get the following error message.
I have tried different databases but the error is the same. Can anyone kindly explain what I'm doing wrong?
{
"user":"bob",
"email":"bob#home.com"
}
Mon May 26 00:54:28.228
SyntaxError: Unexpected token :
Attempting to setup / populate a basic mongo db using the shell, I get the following error message.
I have tried different databases but the error is the same. Can anyone kindly explain what I'm doing wrong?

The code you mentioned is just the document itself. Check out the docs for how insert works. To insert, you should use:
db.collection.insert()
An example of an insert with the document in your example:
db.collection.insert({ "user":"bob", "email":"bob#home.com" })
Edit---
To clarify, you must replace "collection", in the above example with the name of the collection you're trying to insert into.
Also, ensure that you're using the correct database. When you connect to the shell, select your db with:
use testdb

Related

Unable to to copy one Field to another filed Mongo db Native query

Hi guys unable to copy one field value to another field in mongo db update many options, I am using mongo db 3.6, i attached my query below pls help me to overcome this issue.
db.am_task.updateMany(
{"reselleraccountid":{$exists:true}},
{$set:{"reselleraccountid":"$parantid"}}
)
Actual Results:
Expected Result
its should replace its value, not print its key name.

Not able to fetch records from mongodb using presto

I have a mongodb catalog under etc/catalog named mongodb.properties
When I run the presto shell and execute command :
Show tables;
I shows the collections of mongodb but when I run select query it gives me errors.
presto> select * from <catalog>.<schema>.<collection/table>;
Gives error :
java.sql.SQLException: Query failed (#20190429_125534_00001_qxggq): line 1:8: SELECT * not allowed in queries without FROM clause
at io.prestosql.jdbc.PrestoResultSet.resultsException(PrestoResultSet.java:1839)
at io.prestosql.jdbc.PrestoResultSet.getColumns(PrestoResultSet.java:1749)
at io.prestosql.jdbc.PrestoResultSet.<init>(PrestoResultSet.java:118)
at io.prestosql.jdbc.PrestoStatement.internalExecute(PrestoStatement.java:251)
at io.prestosql.jdbc.PrestoStatement.execute(PrestoStatement.java:229)
at io.prestosql.jdbc.PrestoStatement.executeQuery(PrestoStatement.java:78)
at spark_mongo_poc.SparkMongo.process_query(SparkMongo.java:32)
at spark_mongo_poc.SparkMongo.main(SparkMongo.java:76)
Presto> select name from <mongodb>.<schema>.<collection>;
Gives error :
java.sql.SQLException: Query failed (#20190429_125718_00002_qxggq): line 1:8: Column 'name' cannot be resolved
at io.prestosql.jdbc.PrestoResultSet.resultsException(PrestoResultSet.java:1839)
at io.prestosql.jdbc.PrestoResultSet.getColumns(PrestoResultSet.java:1749)
at io.prestosql.jdbc.PrestoResultSet.<init>(PrestoResultSet.java:118)
at io.prestosql.jdbc.PrestoStatement.internalExecute(PrestoStatement.java:251)
at io.prestosql.jdbc.PrestoStatement.execute(PrestoStatement.java:229)
at io.prestosql.jdbc.PrestoStatement.executeQuery(PrestoStatement.java:78)
at spark_mongo_poc.SparkMongo.process_query(SparkMongo.java:32)
at spark_mongo_poc.SparkMongo.main(SparkMongo.java:76)
I want same data as we get when we do db.collection.find({}); which gives me proper result in the form of documents
Please help
I Fixed the above issue by just changing the mongodb collection name
to all Lower Case. There is an issue with presto mongodb catalog that
it doesnot recognise Upper case Lettered collection name and that was
the reason it was not able to identify the collection.
So I changed my collection case to lower and it started working. :)
I had similar issue. I fixed it adding this to catalog configuration.
mongodb.case-insensitive-name-matching=true

mongoDB location of query failing

I am trying to convert a huge set of dates to ISODate objects and storing it in the same collection and field. I am using the following query:
db.collection.find().forEach(function(element){
element.StartTime = ISODate(element.StartTime);
element.StopTime = ISODate(element.StopTime);
db.collection.save(element);
});
The query ran for about 10 minutes, and then gave an error:
2017-06-15T15:48:10.419+0200 E QUERY [thread1] Error: invalid ISO date :
ISODate#src/mongo/shell/types.js:65:1
#(shell):2:23
DBQuery.prototype.forEach#src/mongo/shell/query.js:501:1
#(shell):1:1
I had a look into the entries in the DB and it looks like it did convert a lot of the data set, but I am having troubles now fixing the error and finding the location where it stopped. What I tried is using Studio3T to find where either "StartTime" or "StopTime" has a value of 2017-06-15T15:48:10.419+0200 or at least starts with 2017-06, but as I expected (since there shouldn't be any data from June 2017 in there), it can't find anything.
Now when I run the query again, it gives the error immediately.
My question is if it's possible to find the document in the collection responsible for this error or what the cause might be. Are there recommendations for bulk operations like this preventing these errors?

mongoDB clear documents in collection

Please help with error below when running in mongodDb shell, thanks,
> use mvp_demo
switched to db mvp_demo
> show collections;
1476851599865_IND_TRX
1476851599865_NIN_TRX
1476851599865_SWF_TRX
configuration
> db.1476851599865_IND_TRX.remove({})
2017-02-16T22:34:29.377-0500 E QUERY [thread1] SyntaxError: identifier starts immediately after numeric literal #(shell):1:2
The problem is that mongodb wasn't designed to have a number as the first character in a collection name, similar to how JavaScript doesn't let you begin a variable name with a number. But to get around this limitation please try running the following:
db["1476851599865_IND_TRX"].remove({})

Show Mongodb query projection in the Log

(updated)
I want to see the field restriction (projection) used in a query in the log, not just the query itself, so I can see exactly what's being requested. I've set 'vvvv=true' along with 'verbose=true' in the config file, so given a shell query;
db.col.find({},{Name:1}).count()
I can then see this in the log;
command: {"count":"col, query:{_id:23}, fields: {Name: 1.0}}}
However, the following query does NOT.
db.col.find({_id:23},{Name:1})
Nor am I seeing this through the C# driver when I use Fields.Include on the MongoCursor.
What am I missing?
The MongoD server currently does not log the projection. There is a ticket in our JIRA for this: https://jira.mongodb.org/browse/SERVER-3129 — please vote for it if you want to show this is important to you.