How to get dict of chat members in Telebot? - python

I wanna get dict of all group chat members.
I wanted to dict look like that {"user1": {"userid": 123456789}, "username": "BlaBlaBla", "firstname": "blablabla"}} etc.

Related

How to update MongoDB/PyMongo according to dictionary?

I'm looking to update all documents in a database for users such that each user gets an update similar to
for user in user_to_state:
collection_users.update_one({"username":user}, {$set: {"state":user_to_state[user]}})
But I'd like to do it in a single query rather than iterating through the dictionary updating each one at a time. Is this possible? Is it faster than iterating?
If I have a dictionary of each user's username (a field currently on every document in this collection) and their state e.g. {"jim12":"TX", "jane34":"FL", ...} is there a way to do a singular query that submits the entire dictionary and goes through each or does it have to be iterated through with the update_one method?
Thanks in advance!
You should be able to use bulk write to do this.
queries = []
for user in user_to_state:
query = {
"updateOne": {
"filter": {"username": user},
"update": {"$set": {"state": user_to_state[user]}}
}
}
queries.append(query)
collection_users.bulk_write(queries)
MongoDB docs here.
Pymongo docs here.

How to store list of JSON data in a Django model?

What is the best way to store a list of JSON dictionaries in a Django model field?
I am creating a transaction application for shops and I am trying to store the items of each order in a field without relying on relational models. Each item in this list will have a name, quantity and unit_price.
For example:
[
{
"name": "Product #1",
"quantity": 1,
"unit_price": 25.0
},
{
"name": "Product #2",
"quantity": 1,
"unit_price": 50.0
}
]
I am using a PostgreSQL database, but I've heard it's bad practice to use database-specific fields, such as ArrayField. What would be the best way to go about this and that would also keep the DRF serialization as simple as possible?

DynamoDB access to items inside a dictionary

I am starting using dynamoDB with python, and I have a doubt I can't find an answer online. I have a JSON with this format:
{
"version": "1.0.0",
"trips":[
{
"paymentMode": "ONLINE",
"type":"A",
"locations":{
"name": "Zona A",
"payment":1000
"company": ["XXX", "YYY"]
},
{
"paymentMode": "CASH",
"type":"B",
"locations":{
"name": "Zona D",
"payment":200
"company": ["XXX", "YYY"]
}
]
}
I can store it like that directly but I don't know if there is a way I can access individually each of the elements in trips in a get_item operation or similar? Like, having for example paymentMode and type as primary and sort key, even if they are inside the field "trips".
The input will be a Json like this, but I would like to put those fields as PK (considering they are unique) so I can retrieve only one item.
I know you can just scan everything and then iterate trips, but maybe with more elements, it will take too long. Is there a way to do this directly? if so, how can I do it? does dynamoDb do this automatically or i have to name it like trips.PaymentMode or something like that?
Thank you very much
When designing how to structure your data in a database, you need to consider A) what data you are storing B) how you will be accessing your data.
Additionally, you need to consider the characteristics of the database in questions. For DynamoDB, the main constraint is your primary key. The primary key is either the partition key or the combination of partition key and the sort key, and it must be unique for all items.
Although it's not entirely clear, based on your JSON your items are trips with fields paymentMode, type and locations. If those are your fields on the item, what should be your key? You mention using paymentMode and type, however, these likely won't be suitable, as they probably won't be unique. If you have a time associated with these, a primary key of paymentMode_type and sort key of time might do the job for you, but that depends on the volume of the data.
What you might be better off doing, is assign a unique identifier to each item (e.g. a uuid) and add secondary indices over the fields you want to use to query (paymentMode, type). This will allow you to query efficiently without having to scan the entire database. Do however keep in mind, that you will incur additional costs for the secondary indices.

How to query documents containing an array of objects in Firestore with python

I have a collection, users, that contained a field which is array of object:
{"status":"active", "children": [{"name": "jack", "id": 1234}]}
I'm trying to search on this collection and find the user that children.id equals to 1234.
I tried with this:
db.collection('users').where('children', 'array_contains', {'id': 1234}).get()
and also this:
db.collection('users').where('children.id', '==', 1234).get()
but did not work.
How can I do these kinds of queries on firestore?
Unfortunately you can't query this way and this question was already discussed here. You need to revise your data structures.

MONGODB Searching through a 3rd layer json object without knowledge of the containing dictionary

My DB goes like this
{
"_id" : ObjectId("5f32e5e2928168864d3219c6"),
"company_name" : "Automotive",
"users" :[ {
"Ali" : {
"number" 732928191,
"Mac" : "00A869F32FB8",
}
}]
}
{
"_id" : ObjectId("5f32e5e2928168864d3219c6"),
"company_name" : "medical hospital",
"users" : [{
"Erin" : {
"number" 6063840123,
"Mac" : "00A859F10DB8",
"email": "email#email.com"
}
{ "Ron" : {
"number" 2021230303,
"Mac" : "00B834D133B8",
}
}]
}
And I'm trying to search for the user who owns, lets say the Mac address 00A859F10DB8 without prior knowledge of his "company_name" or "name"
Full disclosure: I work at Rockset. I see that this question can easily be answered by doing integration with MongoDB and Rockset.
Asking for clarification: What kind of data is this? If this real-time data/ doing analytics, MongoDB has a partnership with Rockset. With Rockset, you can do full JOINS, Search, and aggregations on deeply nested data - without ever knowing the shape of your data. The EASIEST WAY to solve this ( by easy, not worrying about indexing, setting up shards, and etc) is to use MongoDB as your primary DB and send data to Rockset.
On Rockset, give READ permissions to your MongoDB (i.e. if you're on Atlas)
Start writing your search query (SQL)
You can read this blog.
Check out this Youtube video
Again, to iterate, I see that your question is easily answered by using MongoDB + Rockset and doing a simple search SQL query.

Categories

Resources