cloudant: update document which replace existing data - cloudant

I have following document
{
"_id": "9036472948305957379",
"_rev":"162de87a696361533791aa7",
"firstname":"xyz",
"lastname": "abc"
}
Now I want to update above dosument to following
{
"_id": "9036472948305957379",
"_rev":"162de87a696361533791aa7",
"name":"xyz abc"
}
if I do doc['name'] = "xyz abc" it doesnt remove firstname and lastname attributes. how do I achieve that?

You need to explicitly remove the firstname and lastname properties from your local copy of the document before saving it back in the database.
If I understand your issue correctly you are currently sending the following document body (implicitly or explicitly) to the database when you initiate the update operation:
{
"_id": "9036472948305957379",
"_rev":"162de87a696361533791aa7",
"firstname":"xyz",
"lastname": "abc",
"name":"xyz abc"
}
However, your payload needs to look as follows:
{
"_id": "9036472948305957379",
"_rev":"162de87a696361533791aa7",
"name":"xyz abc"
}
If you are using the python-cloudant library take a look at the field_set method at http://python-cloudant.readthedocs.io/en/latest/document.html:
static field_set(doc, field, value)
Sets or replaces a value for a field in a locally cached Document object. To remove the field set the value to None.

Related

Azure logic apps: Nullable JSON values not available as dynamic content

I'm building a logic app that pulls some JSON data from a REST API, parses it with the Parse JSON block, and pushes it to Azure Log Analytics. The main problem I'm hitting is an important JSON field can either be an object or null. Per this post I changed the relevant part of my JSON schema to something like this
"entity": {"type": ["object", "null"] }
While this works, I'm now no longer to access entity later in the logic app as dynamic content. I can access all other fields parsed by the Parse JSON block downstream in the logic (that don't have nullable field). If I remove the "null" option and just have the type set to object, I can access entity in dynamic content once again. Does anyone know why this might be happening and/or how to access the entity field downstream?
Through the test, if we use "entity": {"type": ["object", "null"] }, we really cannot directly select entity in dynamic content.
But we can use the following expression to get the entity:
body('Parse_JSON')?['entity']
The test results seem to be no problem:
For a better understanding, let me cite a few more examples:
1. If your json is like this:
{
"entity": {
"testKey": "testValue"
}
}
Your expression is like this:
body('Parse_JSON')?['entity']
2. If your json is like this:
{
"test": {
"entity": {
"testKey": "testValue"
}
}
}
Your expression should like this:
body('Parse_JSON')?['test']?['entity']

Not able to query mongo repository with parameter

I'm trying to query the following object from mongodb
[
{
"id": "6b3a9814c1990a0578988d9e",
"details": {
"buyerId": "5bd450ed0307fa0a3a904376",
"offerId": "1",
"productId": "5b3a9814c1880a0578988d6a",
"productTitle": "Watch",
"amount": 50,
"status": "Open",
}
}
]
I'm using spring-boot-starter-data-mongodb so first, I tried it the standard way.
Here is what's in my repository,
public interface OfferRepository extends MongoRepository<Offer, String> {
List<Offer> findOffersByDetailsBuyerId(String buyerId);
}
I've also tried a custom query,
#Query(value = "{'details.buyerId' : ?0 }")
List<Offer> findOfferByDetails_BuyerId(#Param("buyerId") String buyerId);
Both are coming back with an empty array. But if I hard code the buyerId in the query I get the results I want.
Also, when I debug it, I see the param but with double quotes around it?
screenshot from mongo compass
In MongoUI you should check the datatype of field ID. It is ObjectId not String So you need to pass org.bson.types.ObjectId instead of String in the repository method.
List<Offer> findByDetailsBuyerId(ObjectId buyerId);
ANSWER
#Query("{ 'details.buyerId' : ?0 }")
List<OfferOverview> findOffersByDetailsBuyerId(String buyerId);
but my main issue was passing the buyerId with double quotes.

Generate JSON schema

Im trying a setup a Microsoft flow. In short, I need to take JSON data retrieved from a device, and parse it so that i could reference it in the Flows below. In order to parse, i need to provide the JSON Schema to Flow. Microsoft Flow has an option to generate it from a sample payload (the results returned from the API call), but it's not generating it correctly. I'm hoping someone can help me. I need the correct JSON Schema.
The data returned from the API:
[
null,
[
{
"user_id": 2003,
"user_label": "Test1"
},
{
"user_id": 2004,
"user_label": "Test2"
}
]
]
Scheme generated in Flow from the above sample payload:
{
"type": "array",
"items": {}
}
I then tried to generate the Schema from just the data. That seemed to work, but when the Flow runs, I get a Json validation error.
Tried generating from just the data like this:
{
"user_id": 2003,
"user_label": "Test1"
}
This generated the scheme like this:
{
"type": "object",
"properties": {
"user_id": {
"type": "number"
},
"user_label": {
"type": "string"
}
}
}
So you have 2 things going on, the nested object array, and the null.
You'll need another Parse JSON after the first Parse JSON. And you'll want to filter out the null before the second Parse JSON.
It took me a while to figure out, but I hope this helps.
Start by adding the Parse JSON step to whatever step is outputting the JSON.
Now, filter the array, make sure you use the 'Expression' when comparing with null.
Add the second Parse JSON, you'll notice that you won't have the option to select the output "Item" of the Filter array step, so select 'Parse JSON' - Item for now (we will change this to use the output of the Filter JSON step in a moment)
The step should automatically change to an 'Apply to each'. In the Parse JSON 2, generate the schema with
[
{
"user_id": 2003,
"user_label": "Test1"
},
{
"user_id": 2004,
"user_label": "Test2"
}
]
Then, modify the 'Select an output from previous steps field' and change it (from the Body of the Parse JSON step) to the Body of the Filter Array step
Finally, add an action after Parse JSON 2 and select one of the fields in Parse JSON 2, this will automatically change that step to a nested Apply to each
You should end up with something like this:

Empty Array being returned

I have created an ionic app and I am currently stuck trying to retrieve an array back from MongoDB through Go. This is what the data in MongoDB looks like.
{
"_id": {
"$oid": "58a86fc7ad0457629d64f569"
},
"name": "ewds",
"username": "affe#dsg.com",
"password": "vdseaff",
"email": "fawfef",
"usertype": "Coaches",
"Requests": [
"test#t.com"
]
}
I am currently trying to get back the Requests field one of the ways I tried was trying to receive the whole document using the following code.
//this is the struct being used.
type (
User struct {
Name string
Username string
Password string
Email string
UserType string
Requests []string
}
)
results := User{}
err = u.Find(bson.M{"username": Cname}).One(&results)
This only returns the following with an empty array.
{ewds affe#dsg.com vdseaff fawfef Coaches []}
In your data the Requests field has a capital R. The bson library that converts the mongo document to your struct type has this to say
https://godoc.org/gopkg.in/mgo.v2/bson#Unmarshal
The lowercased field name is used as the key for each exported field, but this behavior may be changed using the respective field tag.
So your options are to either add a tag to your Requests field or change your data to use lowercase requests. If you choose the tag option it would be like
Requests []string `bson:"Requests"`

Modifying certain fields in array of objects

I get some objects from a server that look like this:
{
"id": 1",
"name": "Jim"
}
I toss them in $scope.myThings, modify them in my app, adding some fields, they end up looking like this:
{
"id": 1",
"name": "Jim",
"favoriteFood": "Noodles"
}
Perhaps I get an update from the server, changing Jims name. I'd like to take the array of objects I get back from the server and apply their changes to the list that's in $scope without having to replace the list entirely (ie: $scope.myThings = newData) or having to iterate through comparing IDs.
Is there a method to do this?
You can use angular.merge.
It's working with object and arrays.
usage
angular.merge(dst, src);
dst Object Destination object.
src Object Source object(s).
For example:
var a = [{x:4}]; var b=[{x:2,y:3}]; angular.merge(a,b);
result:
a=[{x: 2,y: 3}]
var a = [{x:4}]; var b=[{x:2,y:3}]; angular.merge(b,a);
result:
b=[{x: 4,y: 3}]

Resources