Get a document from couchbase lite database swift - swift

I want to retrieve a document from database to update it, so I need the document ID, how to get this ID if I didn't create the document with a custom ID (I've used database.createDocument() instead of database.documentWithID("docId"))
Thanks

If you haven't created the document with a custom Id then you can use
some other uniquely identifying property (or properties) of the document to query for the specific document.
Instead of querying and iterating over entire list of documents to find the match, you can create a view with the specified property/properties as index. You can then query for that view with startKey / endKey set to the desired property value. That will return the document of interest.
Alternatively,if it's an option, create the document with a custom ID.

Related

Check multiple Document exists id Firestore Collection

I want check within a list of 10 document IDs how many exists in a collection. One way to do it would store the document ID inside the document and use in operator.
const result = await this.afs.collection(path).where('id', 'in', ['doc1', 'doc2']).get();
Is there a way we can avoid storing the document id inside document.
For this particular query, you can use FieldPath.documentId(). It returns a token that you can use in the field path of the query:
this.afs.collection(path).where(FieldPath.documentId(), 'in', ['doc1', 'doc2'])
I've linked you to the plain JavaScript documentation. If you're using Angular, it might have a slightly different way of getting this token value, but it will be in a class called FieldPath.

Cloud Firestore: check if value exists without knowing field name

I would like to check if a certain value is present in my Cloud Firestore collection through all the present fields and have back the document ID that has at least one field whose value is the one searched.
In this example, the code should give back only 2 records when I look for "Peter": 8cyMJG7uNgVoenA63brG and fnk0kgW7gSBc3EdOYWxD.
I know how to do a search when the field name is known. But in this case, I cannot know the field name at prior.
If you don't know the name of a field, you can't perform any queries against its value. Firestore requires queries to use some index, and indexes always work with the names of fields in your documents.

Ways to refer to a database, collection, and document in MongoDB?

use <databasename> will set a variable db to be the database
specified by <databasename>, so the database can be referred to
the variable db.
I wonder if a collection or document can also be
referred to by a variable, and if yes, how?
Every object in a MongoDB server has an identifier _id. If I am correct, a database, a collection and a document are objects.
How are the identifier of an object used in practice?
Both a database and a collection have a name. So we can refer to a
collection via its name e.g. mydb.mycollection.
Does a document also have a name?
Thanks.
A collection consists of several documents so a document as such do not have any name. _id distinguishes the documents. To fetch any particular document, you can filter it on the basis of _id or the data stored in it.
Referencing the db and then the collection in selected db will give you the required document.

Can mongo document ID format be customised?

I would like to encode some meaning behinds first N characters of every document ID i.e. make first three characters determine a document type sensible to the system being used in.
You can have a custom _id when you insert the document. If the document to be inserted doesn't contain _id, then MongoDB will insert a ObejctId for you.
The _id can be of any type but keeping it uniform for all the documents makes sense if you are accessing from application layer.
You can refer one of the old questions at SO - How to generate unique object id in mongodb

Mongodb - geospacial _id?

I currently have a collection of small documents. Each document has an indexed geospacial field and *the default _id is never used in any query*. There will never be more than one document related to a particular geo location. I think it makes sense to override the default _id, and use the geospacial data for this somehow.
Question is, how do you use geospacial data as the unique id? Is it a case of creating a flat string from the geo field? E.g. 'x123456y123456'?
The _id field is the unique identifier for each document and thus is a needed field. The _id field is generated on document creation automatically if one is not provided. If you can provide this geospaital value when creating the document you should be able to use the string as you suggested, you cannot use an array as the _id value. However please be aware that once a document is created the _id becomes unchangeable. This means that using the _id field as a meaningful index of geospatial data may not be of much value.
Have a look here for more info on the _id field and here for some information about creating geospatial indexes in Mongo