How to specify schema for nested field in BigQuery? - google-app-engine

The data I am working with has a nested field 'location' which looks like:
"location": {
"city": "Amherst",
"region": "NS",
"country": "CA"
},
How can I specify the schema for nested fields using the Java API?
Currently, my code looks like:
List<TableFieldSchema> fields = new ArrayList<TableFieldSchema>();
TableFieldSchema fieldLocation = new TableFieldSchema();
fieldFoo.setName("location");
fieldFoo.setType("record");
TableFieldSchema fieldLocationCity = new TableFieldSchema();
fieldBar.setName("location.city");
fieldBar.setType("string");
...
fields.add(fieldLocation);
fields.add(fieldLocationCity);
TableSchema schema = new TableSchema();
schema.setFields(fields);
This doesn't work as I am getting the following error:
CONFIG: {
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "Record field location must have a schema."
}
],
"code": 400,
"message": "Record field location must have a schema."
}

I think you'd want to do something like the following:
List<TableFieldSchema> inner = new ArrayList<TableFieldSchema>();
List<TableFieldSchema> outer = new ArrayList<TableFieldSchema>();
TableFieldSchema fieldLocationCity = new TableFieldSchema();
fieldLocationCity.setName("city");
fieldLocationCity.setType("string");
// Add the inner fields to the list of fields in the record.
inner.add(fieldLocationCity);
...(add region & country, etc)...
TableFieldSchema fieldLocation = new TableFieldSchema();
fieldLocation.setName("location");
fieldLocation.setType("record");
// Add the inner fields to the location record.
fieldLocation.setFields(inner);
outer.add(fieldLocation);
TableSchema schema = new TableSchema();
schema.setFields(outer);

Related

How to store maps to a firestore array

I have xyz document inside abc collection.
I want to add a map to an array inside document xyz.
I can add a single array item using this code
CollectionReference collection = this.collection(collection);
DocumentReference ref = collection.document(document);
ref.setData({"data": FieldValue.arrayUnion(values)});
here data is the parent array.
but when I try to add another map , firestore replace the values of index 0 , it does not add map to next index means to index 1,2,3 or .....
let see the example
here is the map
values = {
"tokens": "99",
"title": "Cleaning",
"location": {" lat": "34.4333", "lng": "53.4343"},
"id": "gVnGE5ZPRnQ8HYQq7dvT",
"client": "LFBXXj7Zi0xOr0FqBGID",
"date": "22/8/2019",
"message": "request message...",
"budget": "150",
"details": {
" When would you like to have the cleaners over?":
"week",
"What type of cleaning package would you prefer?":
"answer for the type of clean",
"Do you need the cleaning tools and materials?":
"false",
"What time do you prefer to have the cleaners over?":
"8/20/2019"
}
}
and I want to add it as new array item each time when I send data to the firestore
eg.
onTap() {
CollectionReference collection = this.collection(collection);
DocumentReference ref = collection.document(document);
ref.setData({"value": FieldValue.arrayUnion(values)});
}
here is the code
var values = {
"tokens": "99",
"title": "Cleaning",
"location": {" lat": "34.4333", "lng": "53.4343"},
"id": "gVnGE5ZPRnQ8HYQq7dvT",
"client": "LFBXXj7Zi0xOr0FqBGID",
"date": "22/8/2019",
"message": "request message...",
"budget": "150",
"details": {
" When would you like to have the cleaners over?":
"week",
"What type of cleaning package would you prefer?":
"answer for the type of clean",
"Do you need the cleaning tools and materials?":
"false",
"What time do you prefer to have the cleaners over?":
"8/20/2019"
}
}
onTap() {
CollectionReference collection = this.collection(collection);
DocumentReference ref = collection.document(document);
ref.setData({"value": FieldValue.arrayUnion(values)});
}
You are using setData function which overwrites your existing values. You need to use updateData function instead which will add the value to the existing array. Just change your onTap function to this:
onTap() {
CollectionReference collection = this.collection(collection);
DocumentReference ref = collection.document(document);
ref.updateData({"value": FieldValue.arrayUnion(values)});
}

Join 2 collections in mongo with a where clause [duplicate]

I have a collection named Releases that holds a subdocument named product.
My collection looks like this:
{
"_id": ObjectId("5b1010e4ef2afa6e5edea0c2"),
"version": "abc",
"description": "<p>abc</p>\n",
"product": {
"_id": ObjectId("5b100c9949f43c6b6f10a93f"),
"name": "Product 1",
"description": "<p>abc</p>\r\n",
"image": "Product 1.png",
"__v": 0
},
"releasedate": ISODate("2018-05-30T00:00:00Z"),
"__v": 0
}
I am trying to find all releases associated to a specific product.
var id = req.body.productId
var query = {'product._id' : id};
Release.find(query)
.select('_id version description product releasedate')
.sort({releasedate: 1 })
.exec()
.then(releases => {
console.log(releases);
res.status(200).json(releases);
})
But it gives me an empty array if i console.log(releases)
I've been stuck with this for a while and asking u guys for help. What am i doing wrong. I read the documentation on https://docs.mongodb.com/manual/tutorial/query-embedded-documents/ and tried to apply that to my code but i cant get it to work.
My Schema looks like this:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Product = new Schema({
_id: Schema.Types.ObjectId,
name: String,
description: String,
image: String,
});
module.exports = mongoose.model('Product', Product);
You need to change your id from string to mongoose objectId
var id = req.body.productId
var query = {'product._id' : mongoose.Types.ObjectId(req.body.productId)};
Release.find(query)
.select('_id version description product releasedate')
.sort({releasedate: 1 })
.exec()
.then(releases => {
console.log(releases);
res.status(200).json(releases);
})

How to re-structure the below JSON data which is a result set of SQL query using cursors in python

def query_db(query, args=(), one=False):
cur = connection.cursor()
cur.execute(query, args)
r = [dict((cur.description[i][0], value)
for i, value in enumerate(row)) for row in cur.fetchall()]
cur.connection.close()
return (r[0] if r else None) if one else r
my_query = query_db("select top 1 email as email_address,status = 'subscribed',firstname,lasstname from users")
json_output = json.dumps(my_query)
print json_output
Result is this:
[{
"status": "subscribed",
"lastname": "Engineer",
"email": "theengineer#yahoo.com",
"firstname": "The"}]
what I want is this
{
"email_address":"yash#yahoo.com",
"status":"subscribed",
'merge_fields': {
'firstname': 'yash',
'lastname': 'chakka',
}
I don't have any column called merge-fields in database but, I want this merge-fields header for every email-id with first name and last name under it to post it to Mailchimp. what modification do i have to do to my cursor get desired output. Any help will be appreciated. Thanks!
I'm adding this so that future users of mailchimp API version3 can get an idea of how I achieved this.
Here is what i did I've added another function which accepts values in other word I capture all the values which are resulted from myquery and pass it to this below function
def get_users_mc_format(query):
users = query_db(query)
new_list = []
for user in users:
new_list.append({
"email_address": user["email"],
"status": user["status"],
"merge_fields": {
"FNAME": user["firstname"],
"LNAME": user["lastname"],
},
"interests": {
"1b0896641e": bool(user["hardware"]),
}
})
return new_list

How do i compare an as3 string with a json array?

Im trying to compare two specific bits of information. One is in the form of a as3 string the other is in the form of a Json array.
The Json array shows customer data from a shop website. What i want to do is have as3 compare the string(i.e a customer name) with the data , and when it has found the matching name, only trace that customers specific information.
Im amusing id have to use a loop for the comparison , but im having trouble getting my head around how to convert the Json into specific junks that can then be compared individually with a string. Any help would be perfect.
Thanks
Be aware that JSON is just a standard AS3 object. There's no magic going on here; loop through it as you would any other structure and run your comparisons as usual.
Solution
var jsonObj:Object = {
"customers": [
{
"id": "04aa1ab3-521b-11e3-a29a-bc305bf5da20",
"name": "fake name",
"customer_code": "00000002",
"customer_group_id": "6012cd22-5166-11e3-a29a-bc305bf5da20",
"customer_group_name": "All Customers",
"first_name": "test",
"last_name": "test",
"company_name": "",
"email": "testest#yahoo.com"
}
]
}
var myString:String = "fake name";
for (var k:String in jsonObj.customers[0]) {
var v:String = jsonObj.customers[0][k];
if (v == myString) {
trace(myString + "'s email is " + jsonObj.customers[0].email)
}
}
Use a Dictionary Object to store customer objects using their unique names (or ids ) as reference;
You certainly do not want to write unnecessary loop search for a customer in question.
Solution.
var jsonObj:Object = {
"customers": [
{
"id": "04aa1ab3-521b-11e3-a29a-bc305bf5da20",
"name": "fake name",
"customer_code": "00000002",
"customer_group_id": "6012cd22-5166-11e3-a29a-bc305bf5da20",
"customer_group_name": "All Customers",
"first_name": "test",
"last_name": "test",
"company_name": "",
"email": "testest#yahoo.com"
}
]
}
//Dictionary Object to store customers
var customers:Dictionary=new Dictionary();
//customer object in the json
var customer:Object;
//loop json object to retrieve customers and
//store them in Dictionary using either unique name or id
for each(customer in jsonObj)
customers[customer.name]=customer;
//retrieve customer in question
function getCustomer(id:String):Object
{
return customers[id] as Object;
}

ExtJs root node

What is the root property value if I get a Json like that:
{
"status": {
"status": 0,
"msg": "Ok",
"protocolversion": "extjs.json"
},
"value": {
"table": [
[
"admin",
"Administrator",
""
],
[
"test",
"Test",
""
]
],
"total": 2
}
}
The data will be displayed in a gridpanel, 1 row is admin, 1 row is test, etc.
Tried:
value, value.table
How to get this to work?
value.table is correct for the root property, but you are using a json format that I don't think Ext is set up to handle by default. It has a reader for json that is used for an array of objects, not for an nested arrays of field values with no object mapping information.
If you have to use that format, you will either need to create your own readers/writers or just use Ext.Ajax.request(), and in the callback, parse the nested array into objects. Something like:
Ext.Ajax.request({
url: 'path.com/to/content',
success: function (response, operation) {
var data = Ext.JSON.decode(response.responseText);
var fields = data.value.table;
var records = [];
Ext.Array.each(fields, function (fieldArray, fieldIndex) {
Ext.Array.each(fieldArray, function(fieldValue, valueIndex) {
//Create record object if it doesn't exist
var record = records[valueIndex] || {};
//Create a switch statement based on the array index to set fields
switch(fieldIndex) {
case 0:
record.User_id = fieldValue;
break;
case 1:
record.Username = fieldValue;
break;
}
});
});
//Add the objects to the empty store
store.add(records);
}
});
That's not a production solution by any means since it doesn't handle that empty string at the end of your list or the case that you get a jagged array of arrays per field which I can't imagine what to do with. If it's within your control or influence, I would suggest using a format more like what Ext suggests so you can use the built in json proxy/reader/writer or go crazy with it and implement ext's remote procedure call format:
{
"success": true,
"message": "Success",
"data": [
{
"User_id": "admin",
"Username": "Administrator"
}, {
"User_id": "admin",
"Username": "Administrator"
}
]
}
In above example "value" is root property. But for JSON reader it's a property name (or a dot-separated list of property names if the root is nested).
so you can assign into your field you need following.
fields:['table.admin','table.test']

Resources