B4J Question MongoDb Find (_id)

lucdrb

Active Member
Licensed User
Longtime User
Hi,

I try to use the ObjectId made from MongoDb to find some record but I' can't retreive any info.
I've use the following code but no return value

B4X:
Sub Find
    Private l As List
    l.Initialize
    Private ar() As String
    ar  = Array As String ("ObjetctId('5ac65c19d4adb22e305b9f27')")
    l=Inventory.find(CreateMap("_id": CreateMap( "$in": ar)),Null,Null)
    For Each items As String In l
        Log (items)
    Next
End Sub

I've also use this code but no return value either

B4X:
    Private l As List
    l.Initialize
    l= Inventory.Find(CreateMap("_id" : "5ac65c19d4adb22e305b9f27"),Null,Null)
    For Each items As String In l
        Log (items)
    Next
End Sub

I can use the find method for any other records but I can't use it with the _id to find a record.
What do I miss?

Luc
 

lucdrb

Active Member
Licensed User
Longtime User
In the mongo shell the following code works well


Thanks for your help.

Luc
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Never initialize a variable and then assign a different object to it:
B4X:
    Dim ar() As String = Array As String ("ObjetctId('5ac65c19d4adb22e305b9f27')")
   Dim l As List = Inventory.find(CreateMap("_id": CreateMap( "$in": ar)),Null,Null)

You need to use MongoClient.StringToObjectId:
B4X:
Dim l As List = Inventory.find(CreateMap("_id": CreateMap( "$in": Array(client.StringToObjectId("5ac65c19d4adb22e305b9f27")))),Null,Null)
 
Upvote 0

lucdrb

Active Member
Licensed User
Longtime User
Never initialize a variable and then assign a different object to it
Many thanks for the advice.
For the MongoClient, everythink was clear but I had forgot to read all the documentation about Jmongo.
Likely be in hurry to code

Once again thanks for your help.
Luc
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…