154
Views
8
Comments
[MongoDB] MongoDB Integration To OutSystems
Question
Forge component by Barduino
10
Published on 20 Nov 2020

Hi! I'm having a difficulty integrating my MongoDB database using the MongoDB plugin from Forge. I'm doing the same approach as shown in the MongoDB Demo (i.e. creating a different module for the backend and putting the logic in wrappers) but I was not able to get any results from my query. I also created a structure for the data that is expected to be returned by the database. Maybe I'm missing something somewhere in my module. The option of using REST services is not applicable since I was required to use "pure" OutSystems only.

Can someone please help me with this? Thank you!

Rank: #139

Hi Bryan,

A simple MongoDB query will look like this, in Service Studio:

On the MongoDocumentQuery action, you'll have to pass the following parameters:

  • ConnectionString
  • DatabaseName
  • CollectionName
  • FilterJSON

Here's an example:

Afterwards, you'll have to iterate over the query results, deserialize each document into a structure and append it to the resulting list.

In which part of the process are you having problems?

Cumps

David

Rank: #12184

I have a Movie App in which I am trying to retrieve a single item. I supplied the required parameters as shown on the pics below. Yet whenever I am executing the MongoDocumentGet action, I am not getting any result.



This is my query: 

This is the result when I ran the debugger:

I'm wondering if there is something wrong with the parameters I supplied, or if building a "core module", just like in the MongoDB Demo and implementing it in my project is a wrong idea. I have checked the connection string, database and collection names and I'm sure that they are correct.

Rank: #139

Hi again,


The action MongoDocumentGet will try to fetch a Mongo document by its internal identifier (_id).

A equivalent query on Mongo would be:

  db.collection.find( { _id: "your-identifier" });

From what I see, it seems the MovieId you're providing doesn't match with any _id in MongoDB.


Cumps,

David

Rank: #12184

Sorry for the late reply.

I did a query on MongoDB Compass, and the item exists on the database.



As you can see, it is the same identifier that I used on my query below.


Maybe there is something here that I have missed. I'm still figuring it out though.

Rank: #139

Hi Bryan,

Yep it seems correct. Here are a couple of extra things you can check:

  • Are the database and collection names correct?
  • Have you tried querying all documents and check the result?
Rank: #12184

Hi David!

I tried querying all the documents and it did return my desired result. But whenever I try to query using the MongoDocumentGet, it doesn't return anything. I think I'm almost there. I'm still tweaking around the FilterJSON parameter of MongoDocumentQuery, as well as the id parameter of MongoDocumentGet. 

I will update here ASAP. Cheers!

Update: 

I tried the action MongoDocumentQuery and supplied the FilterJSON parameter, this is the result:

It's empty.

It's the same thing with MongoDocumentGet.

I'm starting to think that the culprit is the argument or the value of the id being passed in the id parameter of those actions.

And also, I'm wondering if the version of OutSystems that I am using has something to do with this problem. I am using OutSystems 11.

Rank: #139

Hi Bryan,

What are you passing on the "id" parameter?

Have you tried using MongoDocumentQuery and filtering by other fields?

Regards,

David

Rank: #3928

For what it is worth after 5 months..


Your document is stored with the id being wrapped in ObjectId(id). This is done in the .NET code by the Document_Insert. Also see this page as reference.

The MongoDocumentGet is executing a Find method with the {_id:<your document id>} as query. With just providing the id as

"507c7f79bcf86cd7994f6c0e"

the Find method is not finding your document, since it is stored in the form

ObjectId("507c7f79bcf86cd7994f6c0e")

looking at your provided screenshot.

I think you need to enclose your Id with Object() in order to have it retrieved.