mongodb / stitch-js-sdk

MongoDB Stitch JavaScript SDK
Apache License 2.0
113 stars 67 forks source link

Find By _id With _id String #157

Closed saniyusuf closed 6 years ago

saniyusuf commented 6 years ago

In normal mongo DB, we have the ability to pass the ObjectID as param string and it will be matched. The Stitch client does not seem to support this.

Every example I see relies on the user setting the owner_id manually or via a trigger which is the angle I want to take since that is automated. I also have another ID called profile ID, which is a secondary user table as recommended for metadata.

Anyways Im just asking if there is a more streamlined way to do a simple search the standard mongo DB way.

edaniels commented 6 years ago

stitchClient.auth.user.id contains the user id which is a string. I have not seen the behvaior you're referring to where you can pass a string to match an object id. The only thing similar to that is using an objectid constructor or extended json.

saniyusuf commented 6 years ago

@edaniels My issue is when using the collection.find() I want to be able to pass collection.find({ "_id" : someUserID})

When I pass the string value of the ID, this doesn't work. If you look at most samples, they use a manual field called owner_id. Im asking if there is any way to find by passing the string to the _id property?

edaniels commented 6 years ago

What does your _id refer to in your document? An auto generated id, a stitch user id, or something else? The owner_id field in examples is there because documents are inserted with that field set to the stitch user id manually. There's no automatic process for that.

saniyusuf commented 6 years ago

Let me rephrase my question. Let's say I have a collection called Profiles. Now let say I have the _id for a specific Profile entry in the Profiles collection. Say the entry for You Daniels has an "_id" of 1234.

I would normally in mongoDB be able to do collection.find( { "_id" : 1234 } ) and have your profile returned. However, in Stitch I cannot. So I am asking is this normal or am I missing something.

I resort to having another field called owner_id or something that has the same 1234 for me to be able to find this field. Basically, I am unable to use the _id in stitch to get results.

edaniels commented 6 years ago

I see. Stitch definitely allows you to do that but you need to check your collection rule in the UI to check how a role is selected. A common role is selected by checking owner_id is equal to the current users id. If you make that check or predicate {} then you would be able to see the document.

Can you tell me more about the rule you're using or show me? I recommend checking out https://docs.mongodb.com/stitch/procedures/manage-rules/ as well.

If a rule determines you can't see a document, stitch won't return it even if it stitch can see the document in the database. This is the feature called QueryAnywhere

saniyusuf commented 6 years ago

@edaniels I have all roles disabled on my test account and this doesn't return anything. Our aim is to get everything working on test account then we move to staging before going live.

edaniels commented 6 years ago

Can you click the advanced view and show me the json of your rule?

saniyusuf commented 6 years ago

@edaniels Here you go { "roles": [ { "name": "owner", "apply_when": {}, "insert": true, "delete": true, "read": true, "write": true, "fields": {}, "additional_fields": {} } ], "filters": [], "schema": { "properties": {} } }

That is our dev rules. I would really love to help with the documentation as alot of it is either not up to date or no consistent with the Demo. For example, there are no list of collection functions the client supports. One that got me was that I couldnt us .findOne or that in Mongo .toArray() is used to cursors while for stitch it is .asArray(). These are the sort of non documented got you that has really gotten us moving slow. Happy to jump on a call to further shwocase.

edaniels commented 6 years ago

Sure. Email me at erd@mongodb.com with Skype, Hangouts, or somewhere we can do a remote screen share.

edaniels commented 6 years ago

This ended up being an issue with documentation which we will address!

saniyusuf commented 6 years ago

I can confirm the fix for this is to use the BSON Types as recommended by @edaniels which can be found at https://github.com/mongodb/js-bson

max8hine commented 6 years ago

I also found what MongoDB stitch does not return a cursoršŸ¤”. The document has a bug tho. https://docs.mongodb.com/stitch/mongodb/actions/collection.find/

My question is what does collection return?

davidmaneuver commented 5 years ago

On Topic:

For future reference, this is how I did it with the aforementioned library.

import { ObjectId } from 'bson'
const stringId = '1234'
const objectId = new ObjectId(stringId)

collection.find( { "_id" : objectId } )
GHEMID-Mohamed commented 5 years ago

@davidmaneuver worked for me, thank you :+1: