MongoDB collection shorthand? - mongodb

Newbie alert.All this while I'm using the below command to refer to specific collection as below
db.collection('someCollection')
But example shared on this link, clearly there's a shorthand available as below
db.products
So I go ahead and change it from db.collection('someCollection').update(...) to db.someCollection.update(...), but i'm getting exception
(node:51451) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'update' of undefined

Example from the doc is for mongodb shell. Which, although also using javascript, is a completely different environment from node.js driver. From a brief glance at the driver's documentation, looks like it does not support this sugar/shorthand.

Related

MongoDB Atlas scheduled trigger service returning undefined

I’m trying to create a scheduled trigger to clear a collection weekly, but I am unable to get the service…
const collection = context.services.get('mongodb-atlas'); is returning undefined when I log it to console, and when I try and using it, it just says Cannot access member ‘db’ of undefined. I’ve also tried setting the service name to Cluster0 and mongodb-datalake, neither of which worked.
If someone could lend a hand on what I’m doing wrong and how I’m meant to do this, that would be awesome. Thanks.
You need the following syntax to get to your collection:
const collection = context.services.get(<SERVICE_NAME>).db("db_name").collection("coll_name");
SERVICE_NAME you can find from here:
Go into Realm tab
In the left navigation, click on Linked Data Sources
Copy the Realm Service Name

I can't find MongoDB documents through the shell, but my code is creating/finding documents just fine

I've got a simple NodeJS app going with Mongoose. I can add/find documents in my MongoDB instance in my code, that's all working well. I've got the MongoDB extension for VS Code and that is showing documents in the collection I'm using.
However if I use the MongoDB shell (and I do use <db_name> then use <collection_name>), when I try db.collection_name.find({}) nothing is returned.
When I try db.collection_name.find({"_id":5fc17693bd6abc23ca398268}) I get the following error:
uncaught exception: SyntaxError: identifier starts immediately after numeric literal :
#(shell):1:24
When I try db.collection_name.find({"name":Shares}) where every document has the same value for name so I assume I should get back everything, I get the following error:
uncaught exception: ReferenceError: Sharesies is not defined :
#(shell):1:19
I'm using MongoDB in Docker, on a different computer to what I'm coding on, so maybe I've done something wrong in configuring it. But Mongoose is happy, the VS Code extension is happy but I can't seem to interact at all with the documents in my collection through the Shell.

What is more recommended to use in the C Driver , mongoc_collection_command with "insert" or mongoc_collection_insert

After working for awhile with the C driver , reading the tutorials and the API .
I little confused ,
According to this tutorial : http://api.mongodb.org/c/current/executing-command.html
i can execute DB and Collections commands which include also the CRUD commands.
And i can even get the Document cursor if i don't use "_simple" in the command API
so why do i need to use for example the mongoc_collection_insert() API command ?
What are the differences ? what is recommended ?
Thanks
This question is probably similar to what's the difference between using insert command or db.collection.insert() via the mongo shell.
mongoc_collection_insert() is specific function written to insert a document into a collection while mongoc_collection_command() is for executing any valid database commands on a collection.
I would recommend to use the API function (mongoc_collection_insert) whenever possible. For the following reasons:
The API functions had been written as an abstraction layer with a specific purpose so that you don't have to deal with other details related to the command.
For example, mongoc_collection_insert exposes the right parameters for inserting i.e. mongoc_write_concern_t and mongoc_insert_flags_t with the respective default value. On the other hand, mongoc_collection_command has broad range of parameters such as mongoc_read_prefs_t, skip, or limit which may not be relevant for inserting a document.
Any future changes for mongoc_collection_insert will more likely be considered with the correct context for insert.
Especially for CRUD, try to avoid using command because the MongoDB wire protocol specifies different request opcodes for command (OP_MSG: 1000) and insert (OP_INSERT: 2002).

OrientDB: Creating Graph-database through HTTP-API

It seems it is only possible to create document-type databases via the HTTP-api. The documentation omit's information about whether the database will be created as a graph or a documentdatabase, but when creating a vertext, i get the error:
Super-class V not exists
...which, as i understand, means that the database is a document-type.
Is there any way around this? The /command/ action makes it pretty easy for me to do what I want from C#, if I could only generate a graph-database via HTTP-api...
Is there any way to create a graph-database through the HTTP-API?
Alternatively: Is there another way to create a graph-database from c#? I've tried creating a database via the console, but here i get IllegalArgumentException:
Error: java.lang.IllegalArgumentException: Storage type 'plocal' is different by storage type in URL
..which i cannot find any explenation for
Any help is much appreciated :)
I managed to have that using POST http://localhost:2480/database/<db name>/plocal/graph
I did found it experimentally. Did not found that in documentation which is a bit strange.
Tested on version 2.1.7
I got the same error message when I typed in the following command:
create database REMOTE:localhost/petshop root aaaa plocal document
Solution:
I had to write it in lower case:
create database remote:localhost/petshop root aaaa plocal document

To write js from Server side in MongoDB

Can I write a procedure from server side which later on gets stored in Db and used for further transactions.
If yes can you provide me a sample code which shows how to write the js from server side in java.
Can I write a procedure from server side which later on gets stored in Db and used for further transactions.
No but as #Philipp states you can write a block of JavaScript which will be evaled within the bult in JavaScript engine in MongoDB (spidermonkey atm unles you compile with V8).
I should be clear this IS NOT A STORED PROCEDURE AND IT DOES NOT RUN "SERVER SIDE" as SQL procedures do.
You must also note that the JS engine is single threaded and eval (when used) locks, and about a tonne of other problems.
Really the whole ability to store functions in the system collection is to store repeating code for tasks such as MR.
It is possible to do that, but 10gen advises that you shouldn't do it. Javascript functions can be stored in the special collections system.js and invoced through the eval command.
The rest of this post is copy&pasted from the official documentation: http://www.mongodb.org/display/DOCS/Server-side+Code+Execution#Server-sideCodeExecution-Storingfunctionsserverside
Note: we recommend not using server-side stored functions when possible. As these are code it is likely best to store them with the rest of your code in a version control system.
There is a special system collection called system.js that can store JavaScript functions to be reused. To store a function, you would do:
db.system.js.save( { _id : "foo" , value : function( x , y ){ return x + y; } } );
_id is the name of the function, and is unique per database.
Once you do that, you can use foo from any JavaScript context (db.eval, $where, map/reduce)
Here is an example from the shell:
> db.system.js.save({ "_id" : "echo", "value" : function(x){return x;} })
> db.eval("echo('test')")
test
See http://github.com/mongodb/mongo/tree/master/jstests/storefunc.js for a full example.
In MongoDB 2.1 you will also be able to load all the scripts saved in db.system.js into the shell using db.loadServerScripts()
>db.loadServerScripts()
>echo(3)
3
Well you can use morphia. It is an ODM for mongo in Java. I know it is not like a PL/SQL kind of solution which you exactly want. But mongo has no extensive support for constraints or triggers as in Oracle or SQL Server. Validations and stuff needs to be done through code. ODM's like Morphia in Java, Mongoose in Node.js or Mongoengine in Python are pretty decent emerging libraries you can use for such tasks, but these constraints are on app side.