Firebase python insert - python

Following the official documentation from firebase i found how to create insite collection a new document with attributes.
doc_ref = db.collection(u'statuses').document()
doc_ref.set({
u'power': -1
})
But on farebase, insite a document, you can put attributes also you can place new collection (as attribute).
My question is how can i add a new collection with my own ID and insite this new collection with new attributes?
I just created an example on firebase to show what i don't know how to add it using python.

This is the answer:
room_a_ref = db.collection(u'rooms').document(u'roomA')
message_ref = room_a_ref.collection(u'messages').document(u'message1')

Related

Is there a way to attach policy tags to a Bigquery table?

I've been trying to attach policy tags to existing BigQuery tables using GCP's Python client libraries but can't seem to find a way.
I'm aware of how to create taxonomies and policy tags within PolicyTagManager
But I can't seem to attach policy tags.
I found 'google.cloud.bigquery.schema.PolicyTagList' but I'm not sure where the tableID gets specified or how will policy tags be attached using this method?
Appreciate the help in advance!
Assumption: Keeping column names same before and after updating each with policy tags
Steps:
Pull the table using get_table() and iterate over it's schema (list of SchemaField)
Create new SchemaField object(s), add parameter values by referring to SchemaField's name/description (from step#1) except for policy_tags (type = PolicyTagList
example:
google.cloud.bigquery.SchemaField(name=column.name, field_type=column.field_type, policy_tags=google.cloud.bigquery.PolicyTagList(["projects/some-project/locations/us/taxonomies/123454321/policyTags/180000"]))
Append these new SchemaFields objects into a list and re-assign the table's schema with this new list
use update_table to update schema such as:
google.cloud.bigquery.Client(project = BIGQUERY_PROJECT).update_table(table, ['schema'])

"Update" _id in mongodb document

PS: I only have to do this due to business requirements
I'm able to achieve it using mongosh, but since there are multiple records to be updated, I'm trying to implement a simple python script to automate the task.
Is it possible to do this with pymongodb?
// store the document in a variable
doc = db.clients.findOne({_id: ObjectId("4cc45467c55f4d2d2a000002")})
// set a new _id on the document
doc._id = ObjectId("4c8a331bda76c559ef000004")
// insert the document, using the new _id
db.clients.insert(doc)
// remove the document with the old _id
db.clients.remove({_id: ObjectId("4cc45467c55f4d2d2a000002")})
I'm not able to set the new Id in the doc variable in order to insert the new document that will mirror the old one.
Thanks
Here's a pymongo equivalent:
from pymongo import MongoClient
from bson import ObjectId
db = MongoClient()['mydatabase']
old_doc_id = '4cc45467c55f4d2d2a000002'
new_doc_id = '4c8a331bda76c559ef000004'
doc = db.clients.find_one({'_id': ObjectId(old_doc_id)})
if doc is not None:
# set a new _id on the document
doc['_id'] = ObjectId(new_doc_id)
# insert the document, using the new _id
db.clients.insert_one(doc)
# remove the document with the old _id
db.clients.delete_one({'_id': ObjectId(old_doc_id)})

Unable to update document without specify Primary Key

schema.py:
class Test(Document):
_id = StringField()
classID = StringField(required=True, unique=True)
status = StringField()
====================
database.py:
query = schema.Test(_id = id)
query.update(status = "confirm")
Critical error occured. attempt to update a document not yet saved
I can update the DB only if I specify _id = StringField(primary_key=True), but if I insert a new data, the _id has to be inserted by me instead of automatically created by MongoDB.
Anyone can help me with a solution?
Thanks!
Inserts and updates are distinct operations in MongoDB:
Insert adds a document to the collection
Update finds a document in the collection given a search criteria, then changes this document
If you haven't inserted a document, trying to update it won't do anything since it will never be found by any search criteria. Your ODM is pointing this out to you and prevents you from updating a document you haven't saved. Using the driver you can issue the update anyway but it won't have any effect.
If you want to add a new document to the database, use inserts. To change documents that are already saved, use updates. To change fields on document instances without saving them, consult your ODM documentation to figure out how to do that instead of attempting to save the documents.

How to retrieve the newest record from a Firebase Application and pass to a python variable?

I need to update the variables from a FirebaseApplication object up in real-time.
But a FirebaseApplication is like a static dictionary object and the values can only be obtained by specifying the 'key' by get() method. How can I update the FirebaseApplication all the time? If each value of the firebase object is a dictionary with date/time as keys, how can I retrieve the newest values?
from firebase import firebase
url = "https://dw-1d-cc5-grp08.firebaseio.com/" # this is a test url
token = "yurddml214RsOPi9Ua0OhuDgeuzxm2Q7v7V6T6ZZ" #this is a test token
firebase = firebase.FirebaseApplication(url,token)
output_data = firebase.get('/humidity')
print(output_data)
In addition to accessing data by key, you can order and filter data by one of its child properties. For example:
https://yourdb.firebaseio.com/users.json?orderBy="name"&equalTo="Yannis"
This URL will give you all users whose name property is equal to "Rahul".
See the Firebase documentation on ordering and filtering for more examples.
You can use Firebase functions to get it done.

How to add multiple values to a multi value drop down field in Rally using python and pyral

I'm trying to update a multi value drop down list field (custom field) in Rally using python2.7 and pyral (v 1.3.2).
I can add a single value to the field like this:
>>> update_data = {'FormattedID' : 'US58848', 'c_MapProfile' : {'ObjectID' : 129751972040}}
>>> rally.post('UserStory', update_data)
<pyral.entity.HierarchicalRequirement object at 0x6fffea46c50>
This updates the field to the attribute value specified by the ObjectID.
The object ID in the MapProfile dict points to the AllowedAttributeValue object.
However, I don't seem to be able to figure out, how to add multiple values to the field.
Can anybody give a hint? Or does pyral not support multi-value fields?
Thanks
Thomas
I'm not familiar with the pyral toolkit, but I can say the api expects the data in a certain shape.
'c_MapProfile':[
{'_ref':'/allowedattributevalue/123'},
{'_ref':'/allowedattributevalue/124'}]
I believe you can retrieve the allowed values for your custom field using the getAllowedValues function
Keep in mind this will overwrite any existing values. If you only wish to add or remove entries without overwriting the current value - use the collection endpoints as explained in the CA Agile Central Web Services API Documentation

Categories

Resources