Query In MongoDb while Insertion - mongodb

I use a function insertOne to insert an object. I have place correct colon and also correct commas then why this error come. Thanks in Advance.
This is my code
db.items.insertOne({Name:"Oppo",Price:"45000",Ritems:"345",Sold:"45","Rating:"3.5",once"twos"})
Error is this
uncaught exception: SyntaxError: missing : after property id :
#(shell):1:78
>

I think that your "" are misplaced. I think this way would be better ??
db.items.insertOne({Name:"Oppo",Price:"45000",Ritems:"345",Sold:"45",Rating:"3.5",once:"twos"})
Also the error message is telling you about the Id column, you should check if you need to set it yourself or if your database has an auto-increment ID.

Related

Trying to write a simple code in mongoldb, receiving an error

Just trying out MongoDB. So this might be a very basic issue. Trying to create a collection "info", but encountering errors as below. Appreciate the help.
uncaught exception: SyntaxError: missing : after property id :
#(shell):1:42
The code is as below :
db.info.insert({"userName":"John","mail”:"John#gmail.com","mobile":12345678},{"Transaction":[{"itemId":"a100","price":200},{"itemId":"a110","price":200}]},{"Payment":["Type":"Credi
t-card","Total":400,"Success":true]},{"Remarks":"1st complete record,payment successful"})
When you try, most of the developers would recommend to read the documentation of Insert data in mongodb. If you clearly understand, then you might understand the problem. The document was not well formatted which means you created object for each key:value pair. And payment is an array which holds objects. It was also not formatted.
You can try below code, and have fun with MongoDB
db.info.insert(
{
"userName":"John",
"mail":"John#gmail.com",
"mobile":12345678,
"Transaction":[{"itemId":"a100","price":200},{"itemId":"a110","price":200}],
"Payment":[{"Type":"Credit-card","Total":400,"Success":true}],
"Remarks":"1st complete record,payment successful"
}
)

Add a missing key to JSON in a Postgres table via Rails

I'm trying to use update_all to update any records that is missing a key in a JSON stored in a table cell. ids is the ids of those records and I've tried the below...
User.where(id: ids).
update_all(
"preferences = jsonb_set(preferences, '{some_key}', 'true'"
)
Where the error returns is...
Caused by PG::SyntaxError: ERROR: syntax error at or near "WHERE"
LINE 1: ...onb_set(preferences, '{some_key}', 'true' WHERE "user...
The key takes a string value so not sure why the query is failing.
UPDATE:
Based on what was mentioned, I added the parentheses and also added / modified the last two arguments...
User.where(id: ids).
update_all(
"preferences = jsonb_set(preferences, '{some_key}', 'true'::jsonb, true)"
)
still running into issues and this time it seems related to the key I'm passing
I know this key doesn't currently exist for the set of ids
I added true for create_missing so that 1 isn't an issue
I get this error now...
Caused by PG::UndefinedFunction: ERROR: function jsonb_set(hstore, unknown, jsonb, boolean) does not exis
some_key should be a key in preferences
You're passing in raw SQL so you are 100% responsible for ensuring that is actually valid SQL. What you have there isn't. Check your parentheses:
User.where(id: ids).
update_all(
"preferences = jsonb_set(preferences, '{some_key}', 'true')"
)
If you look more closely at the error message it was telling you there was a problem precisely at the introduction of the WHERE clause, and right after ...true' so that was a good place to look for problems.
Syntax errors like this can be really annoying, but don't forget your database will usually do its best to pin down the place where the problem occurs.

"Unclosed quotation mark" error using entity framework

I'm trying to create DB using code first with entity framework.
When I do this:
myDbContext.DataBase.Create();
I'm getting this exception message:
"Unclosed quatation mark after the character string '(' "
Any idea?
My guess is that somewhere in the data that you are loading when you create the database that one of the strings you're trying to insert has an apostrophe in it. You could Check through the any data that my be called during the Initializer and comment it out in sections until you find which record(s) may be causing the offending message.

MongoDB Shell - access collection with period in name?

I have found a collection in one of our MongoDB databases with the name my.collection.
Is there a way to access this collection from the MongoDB shell, despite it having a point in the name?
> db.my.collection.findOne();
null
I'm pretty sure that that is not correct.
try this instead:
db["my.collection"].findOne();
you run into the same issue with hyphens or any other name that does not match on
[a-zA-Z_$][0-9a-zA-Z_$]
This limitation comes from valid named for javascript object properties.
if collection name is "my.collection"
db.my.collection.findOne(); // OK
null
if collection name is "my.1.collection"
db.my.1.collection.findOne(); // Not OK
SyntaxError: missing ; before statement
Fix:
db["my.1.collection"].findOne(); // Now is OK
null
Another foolproof method is:
db.getCollection("_SCHEMA").find()
While in the case of a underscore in the name, still cause a error with #Laura answer:
> db["_SCHEMA"].find()
2016-07-18T17:44:16.948+0200 E QUERY [thread1] TypeError: db._SCHEMA is undefined :
#(shell):1:1
Your code is correct. If it returns null it means that the collection is empty.

sqlite issue using WHERE statement on a varchar field in Objective-C

I have this sql statement SELECT * FROM Language WHERE LanguageCode=? where LanguageCode is a varchar(2).
When I use sqlite3-prepare-v2 on that statement I got SQLITE_ERROR but if I use SELECT * FROM Language WHERE LanguageID=? where LanguageID is the primary key numeric identity field the statement compiles just fine.
Is there any issue filtering by a varchar field using WHERE statement?
Thanks
I think there should be no issue with the where statement by using a varchar field.
Maybe you are having problem with passing the parameter. Try to print to NSLog and see the statement that is executed, and execute it with the sqlite command prompt to see if you have return values.
Be sure your ? is wrapped in single quotes.
SQLite should also give you an error string which tell you why there is an error.
Try to call sqlite3_errmsg and check the error string returned from that call. It should give you a more detailed information about what went wrong.
Claus