I'm trying to query a mongodb collection with documents containing both an _id and and id field (I know this is not an ideal db design, but I don't own the db).
Is there a way to access the _id field using MongoEngine in Flask?
doc['id'] returns the id field and doc['_id'] throws a KeyError.
Related
I am trying to get all the data from a model and its foreign keys and I have a hard time using select_related() and values() or values_list().
I got an Order table who got two foreign keys. I need to query all the data and send it as a JsonResponse. This is the line:
Order.objects.select_related('persoana_fizica__persoana_juridica').values()
I saw I can write inside values what fields I need, I am looking for a method to select all of them without writing every single one, 'all' didn't worked. Any ideas?
i am using python with firebase SDK, and have a table named jobs, each record, has a field named client, that is a map, each client has an id field. I would like to query the table for all the jobs that have the client with a certain id value, I found this explaining for how to query by array members but can't find anything about query by values of map fields.
will something like
.where("client.id", "==", id) work and be effective? how can I do this query in an effective way? create index maybe?
enter code here
It should work without creating an index. What you wrote should filter on the id property of the client field object for all documents of a collection.
See also:
Firestore: Query documents by property of object
Query Google Firestore database by custom object fields
According to Django documentation if a field is trying to be reached in raw query set, it would fetch it in real time.
How can I prevent it from fetching fields not being retrieved from the database?
e.g. if I write select name from authors
and later a user will write author.gender it would return None and not attempt to retrieve it from the database?
You could try fetching null for any fields that you do not want to be retrieved, for example:
Person.objects.raw('SELECT id, name, null AS gender from authors')
I don't want to create a custom "id" field in MongoDB and make it my primary key.
But I want to override the "_id" field and make it an integer primary key so that every time I post a new document, it should have a integer id rather than ObectID.
Can it be possible by overriding some code in MongoEngine(PyMongo)? or this is the core functionality of MongoDB?
I've tried to insert documents directly in MongoDB database(without using Django), specifying the integer "_id" and it saves the document with no problem. However, when I try to query that particular document then it gives no result.
Same happens with Django, when I hit the API endpoint to see the list of documents it shows the one which has integer id.
But when I try to access that particular one, it says "no result found"
How can I deal with this, please advise.
Note: I'm new to both Django and MongoDB.
When defining a document I used the unique=True attribute on a field to ensure I had such an ID. I'm uncertain if this is your goal.
class Post(Document):
p_id = StringField(min_length=64, max_length=64, required=True, unique=True)
I was using a 64character sha256 hexdigest as the p_id
to insert a row to a table that has a one-to-one relationship, you would do this in Django:
mypk=2 # Comes from the POST request
model=MyModel(myField="Hello", myForeignModel=ForeignModel.objects.get(pk=mypk))
model.save()
This will cause a SELECT query followed by an INSERT query.
However, the SELECT query isn't really necessary as it will be the mypk that is inserted into the foreign key field. Is there a way to get Django to just insert the primary key without doing a SELECT?
Secondly, are there concurrency issues here (in the event that the primary key would change before the user submits the request). If so, how are these dealt with?
From the docs:
Behind the scenes, Django appends "_id" to the field name to create its database column name.
Simply set myForeignModel_id to the FK value.