Flask restful parser list dictionary - rest

I have an example parse request list dictionary:
{
"shopping_cart": [{
"id": 23323,
"qty": 10
}, {
"id": 34232,
"qty": 9
}, {
"id": 34232,
"qty": 9
}]
}
How can i parse it use flask_restful RequestParser ?

Use something like this :)
self.postreqparse = reqparse.RequestParser()
self.postreqparse.add_argument("shopping_cart", type=dict, action="append")

Related

Loopback 3 query by Property of a embedded model

I'm using loopback 3 to build a backend with mongoDB.
So i have 2 models: Object and Attachment. Object have a relation Embeds2Many to Attachment.
Objects look like that in mongoDB
[
{
"fieldA": "valueA1",
"attachments": [
{
"id": 1,
"url": "abc.com/image1"
},
{
"id": 2,
"url": "abc.com/image2"
}
]
},
{
"fieldA": "valueA2",
"attachments": [
{
"id": 4,
"url": "abc.com/image4"
},
{
"id": 5,
"url": "abc.com/image5"
}
]
}
]
The question is: how can i get Objects with attachments.id=4 over the RestAPI?
I have try with the where and include filter. But it didn't work. It look like, that this function is not implemented in loopback3, right?
I have found the solution. It only works on Mongodb, Cloudant and Memory database.
{
"filter": {
"where": {
"attachments.id": 4
}
}
}

MongoDB query for Find 2 levels object element

I have a big issue, i don't know what to do...
What I wanna is to find all objects with Object2 name. I have Object 2 with name element.
What I wanna is to find all objects with the value X in the element name inside Object2. in the example is the value name is ="IWANTALLOBJECTSWITHTHISNAME"
the Json structure.
"objects": [
{
"_id": "5c69a62cf9acf00d00dbc02d",
"date": "2222-02-24T00:00:00.000Z",
"description": "22",
"Object1": {
"_id": "5c69a62cf9acf00d00dbc02b",
"date": "2222-02-24T00:00:00.000Z",
"user": "5c30fd5890bbd24a1c46c7ee",
"positionsObject1": [
{
"id": 1,
"Object2": {
"_id":"5c69a62cf9acf00d00dbc02c",
"name": "IWANTALLOBJECTSWITHTHISNAME"
},
"description": "22",
"value": 22
}
],
"id": 13,
"__v": 0
},
"user": "5c30fd5890bbd24a1c46c7ee",
"id": 7,
"__v": 0
}
]
I'm new in mongoDB and this query is really really hard. I tried everything. Thank very much for the help.
You can specify the path using dot notation:
db.col.find({ "objects.Object1.positionsObject1.Object2.name": "IWANTALLOBJECTSWITHTHISNAME" })

MongoDB, remove nested doc in an array

I have the following structure in MongoDB and I try to remove the documents that contains specific tags. I can't seem to be able to get the $pull work.
In this example, I would like to pull the nested doc that has has tags :["BB"]
Any help will be appreciated !
{
"_id": 123,
"socialItems": {
"facebook": [{
"name": "firstFacebook",
"id": 2
}, {
"name": "secondFB",
"id": 43
}],
"instagram": [{
"name": "firstNstagram",
"id": 4
}],
"pc": [{
"name": "firstPC",
"id": 55,
"tags": [
"ab"
]
}, {
"name": "secondPC",
"id": 66,
"tags": [
"BB"
]
}]
}
}
I assume you are trying to drop the nested 'pc' doc, from the array? You also don't mention if you're using a specific driver for this, so I've assumed you're running this in the Mongo shell.
The following will remove documents from the 'pc' property, when containing the 'BB' tag.
db.collectionName.update({'socialItems.pc.tags': 'BB'}, {$pull: {'socialItems.pc': {tags: 'BB'}}})

Remove entry in an array of a MongoDB document

Say I have a document that looks something like this:
{
"_id": ObjectId("50b6a7416cb035b629000001"),
"businesses": [{
"name": "Biz1",
"id": ObjectId("50b6bc953e47dc923e000001")
}, {
"name": "Biz2",
"id": ObjectId("50b6ccebae0513bf52000001")
}, {
"name": "Biz3",
"id": ObjectId("50b6d015c58b414156000001")
}, {
"name": "Biz4",
"id": ObjectId("50b6d0c8a4cdd5e356000001")
}]
}
I want to remove
{
"name": "Biz3",
"id": ObjectId("50b6d015c58b414156000001")
}
from the array of businesses. I tried this (using business name instead of id for clarity):
db.users.update({'businesses.name':'Biz3'},{$pull:{'businesses.name':'Biz3'}})
but of course it didn't work. I know that the query part is correct because I get the document back when I do this:
db.users.find({'businesses.name' : 'Biz3'})
So the problem is with the update part.
Just ran a quick lil test and this works
I think trying db.users.update({'businesses.name':'Biz3'},{$pull:{'businesses':{'name':'Biz3'}}}) should do it

RestKit 0.10 Object Mapping with JSON for iPhone

I've spent many hours reading the documentation and tried out various sample projects etc. but I just can't solve my problems. As Restkit changed many APIs a few days ago much of the information provided and the tutorials is useless. The stuff that works does not help me with my problems.
In General, I don't understand when to use /person or /person.user or /:user or /person/user when doing the object mapping. I havent found anything about how to access certain JSON entries. Mybe someone can explain this to me.
I want to create objects out of my JSON Response. The Problem is using objects or arrays, nested in an object.
Here is the JSON that causes problems:
{
"result":
{
"persons":
[
{
"age": 20,
"name": "alice",
"user":
{
"id": 208727870
}
},
{
"age": 25,
"name": "bob",
"user":
{
"id": 308427890
}
}
]
}
}
For this sample I need to do the object mapping -> but how?
This mapping works:
{
"result":
[
{
"age": 20,
"name": "alice",
"user":
{
"id": 208727870
}
},
{
"age": 25,
"name": "bob",
"user":
{
"id": 308427890
}
}
]
}
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[RKTUser class]];
[userMapping mapKeyPath:#"id" toAttribute:#"userID"];
RKObjectMapping* statusMapping = [RKObjectMapping mappingForClass:[RKTStatus class]];
[statusMapping mapKeyPath:#"age" toAttribute:#"age"]
[statusMapping mapKeyPath:#"name" toAttribute:#"name"]
[statusMapping mapRelationship:#"user" withMapping:userMapping];
statusMapping.rootKeyPath = #"result";
// user seems only to be a placeholder .. it does not matter, which string i write instead "user"
[objectManager.mappingProvider setObjectMapping:statusMapping forResourcePathPattern:#"/:user"];
What I finally want to do:
{
"result":
{
"id_str": "208727870",
"result_value_str": "123456",
"persons":
[
{
"age": 20,
"name": "alice",
"user":
{
"id": 208727870
}
},
{
"age": 25,
"name": "bob",
"user":
{
"id": 308427890
}
}
],
"friends":
[
{
"age": 18,
"name": "norton",
"user":
{
"id": 208457120
}
},
{
"age": 41,
"name": "edward",
"user":
{
"id": 378829810
}
}
]
}
}
Has someone an advice for me? (tutorials, links, hints…)
Best regards,
Steve