Firebase.util.NormalizedCollection merge multiple paths - angularjs

I would like to know if it is possible with Firebase.util.NormalizedCollection to merge 3 paths or more who depends on each other. On firebase doc it said that the best practice with our datas is to have flattened data as possible, so I have that :
question :
uniqueIdqestion1 :
myquestion : "ma question",
answers : {
uniqueIdanswer1 : true,
uniqueIdanswer54 : true
}
answer :
uniqueIdanswer1 :
my_answer : "my answer",
people : {
uniqueIdpeople1 : true
}
people
uniqueIdpeople1 :
name : "pedra"
With Firebase.util.NormalizedCollection, I would like to list all the answer for my question and have an array like that :
key_answer : uniqueIdanswer,
my_answer : "ma response",
name : "pedra"
I tried to have something like that with this code :
var ref = firebase.database().ref();
var nc = new firebase.util.NormalizedCollection(
ref.child('questions/' + $state.params.chatId + '/answers'),
[ref.child('answers'), 'widget2'], // B path
... ??? // C path
)
.select('widget2.my_answer', ???).ref();
$scope.reponses = $firebaseArray(nc);
But I can't still understand how to merge the B and C path. Maybe it's because C doesn't refer directly to the main path, I don't understand because something is missing for me in my mind ... Please can you help me with that ?

Related

updating path creates write conflict in mongodb

Why use of $[] with $ is causing write conflict ?
db.projectionTesting.updateOne({"metaData.title": "BCDe"} , {$set : {
"metaData.0.title" : "efde" ,
"metaData.$[].hasUpdateddd": 76
}} )
WriteError({
"index" : 0,
"code" : 40,
"errmsg" : "Updating the path 'metaData.$[].hasUpdateddd' would create a conflict at 'metaData'",
"op" : {
"q" : {
"metaData.title" : "BCDe"
},
"u" : {
"$set" : {
"metaData.0.title" : "efde",
"metaData.$[].hasUpdateddd" : 76
}
},
"multi" : false,
"upsert" : false
}
}) :
Why use of $[] with $ is causing write conflict ?
if these are working fine then why not the above one. I want to know what exactly the problem is with the use of $[] with $ in while performing updateOperation
db.projectionTesting.updateOne({"metaData.title": "BCDe"} ,
{$set : {"metaData.0.title" : "efde" ,
"metaData.$.hasUpdateddd": 76}} )
db.projectionTesting.updateOne({"metaData.title": "BCDe"} ,
{$set : {"metaData.$.title" : "efde" ,
"metaData.$.hasUpdateddd": 76}} )
In order to ensure consistency among replica set members, MongoDB replication requires that operations added to the oplog be idempotent. To achieve this update operations such as $inc, $dec, and most array operations are stored in the oplog as $set with the new value of the field.
When combining 2 updates using metaData.$, you are making updates to one specific element.
When combining metaData.0 and metadata.$[] you are making unrelated updates a single element and to the entire array.
The query executor has not been given logic to be able to ensure an idempotent operation in that situation, so it balks with a write conflict.

How to update values in a nested json file by Powershell

I want to update some values of below content which i believe a json file. This is the output of azure devops release definition and I need to reuse the content modifying some fields.
I can simply update values like id and name using these lines
$ReleaseDef.Name = $newReleaseName
$ReleaseDef.path = $folderQA
But I dont know how to update array fields like artifact and triggers. I can get values by calling $ReleaseDef.artifacts.sourceid
but can not set any values there , It throws errors like The property 'sourceid' cannot be found on this object. Verify that the
property exists and can be set.
Please suggest
source : userInterface
revision : 4
description :
createdBy : #{displayName=Jyotiprakash Nayak; url=https://azuredevopsdv
.ril.com/EntApps/_apis/Identities/2ca8bd5d-7797-4177-b7bb-2
6daa0d29ed9; _links=;
id=2ca8bd5d-7797-4177-b7bb-26daa0d29ed9;
uniqueName=domain\Jyotiprakash.Nayak; imageUrl=https://azuredev
opsdv.company.com/EntApps/_apis/GraphProfile/MemberAvatars/win.
Uy0xLTUtMjEtMjIwNzU5NTE2Ni03MjEyNTY2NjUtNTU2MTkwNDkyLTQ4NjY
yMw; descriptor=win.Uy0xLTUtMjEtMjIwNzU5NTE2Ni03MjEyNTY2NjU
tNTU2MTkwNDkyLTQ4NjYyMw}
createdOn : 2019-12-10T09:11:11.057Z
modifiedBy : #{displayName=Jyotiprakash Nayak; url=https://azuredevopsdv
.company.com/EntApps/_apis/Identities/2ca8bd5d-7797-4177-b7bb-2
6daa0d29ed9; _links=;
id=2ca8bd5d-7797-4177-b7bb-26daa0d29ed9;
uniqueName=Domain\Jyotiprakash.Nayak; imageUrl=https://azuredev
opsdv.comapny.com/EntApps/_apis/GraphProfile/MemberAvatars/win.
Uy0xLTUtMjEtMjIwNzU5NTE2Ni03MjEyNTY2NjUtNTU2MTkwNDkyLTQ4NjY
yMw; descriptor=win.Uy0xLTUtMjEtMjIwNzU5NTE2Ni03MjEyNTY2NjU
tNTU2MTkwNDkyLTQ4NjYyMw}
modifiedOn : 2019-12-13T09:16:13.463Z
isDeleted : False
variables :
variableGroups : {}
environments : {#{id=7; name=Stage 1; rank=1; owner=; variables=;
variableGroups=System.Object[]; preDeployApprovals=;
deployStep=; postDeployApprovals=;
deployPhases=System.Object[]; environmentOptions=;
demands=System.Object[]; conditions=System.Object[];
executionPolicy=; schedules=System.Object[];
currentRelease=; retentionPolicy=; processParameters=;
properties=; preDeploymentGates=; postDeploymentGates=;
environmentTriggers=System.Object[]; badgeUrl=https://azure
devopsdv.comapny.com/EntApps/_apis/public/Release/badge/e0b1a36
0-01a5-4eea-af57-b2a461559ac9/7/7}}
artifacts : {#{sourceId=e0b1a360-01a5-4eea-af57-b2a461559ac9:48;
type=Build; alias=_eCAM-Team-CI; definitionReference=;
isPrimary=True; isRetained=False}}
triggers : {#{artifactAlias=_eCAM-Team-CI;
triggerConditions=System.Object[];
triggerType=artifactSource}}
releaseNameFormat : Release-$(rev:r)
tags : {}
pipelineProcess : #{type=designer}
properties : #{DefinitionCreationSource=}
id : 7
name : Release-Template-1
path : \QA
projectReference :
url : https://azuredevopsdv.company.com/EntApps/e0b1a360-01a5-4eea-af
57-b2a461559ac9/_apis/Release/definitions/7
_links : #{self=; web=}
These attributes contain hash table values.
You should be able to update by specifying the key and its new value like so:
$ReleaseDef.artifacts["sourceid"] = "someValueHere"
Thanks all for your valuable helps, I finally made it work through some foreach loops.
```$artifact = $json.artifacts
foreach ($value in $artifact)
{
$value.alias = "_$newReleaseName"
$value.sourceId = "$($projectid):$($BuildID)"
}
foreach ($triggervalue in $snapshot.triggers)
{
$triggervalue.artifactAlias = "_$newReleaseName"
}```

Manipulating Mono and Flux transformation

I'm using Spring 5 webflux with ReactiveMongoRepositories.
Suppose I have 2 collections "Race" and "RaceParticipation":
Race : {
"_id" : "ID",
"name" : "Name"
}
RaceParticipation : {
"_id" : "ID",
"userId" : "ID",
"raceId" : "ID"
}
I need to find a page of Races in which a given user has participated.
The solution I went with is call raceParticipationRepository.findByUserId("userId", PageRequest.of(0,10))
This returns a Flux<UserParticipation>.
The I should collect the ids from the response and call raceRepository.findAllById(ids).
Here is a snippet:
Flux<Race> findUserRaces(String userId, int page){
return raceParticipationRepository.findByUserId(userId, status,
PageRequest.of(page, 6))
.collectList()
.map(list -> list.stream()
.map(ParticipationRepository::getRaceId)
.collect(Collectors.toList()))
.flatMap(list -> raceRepository.findAllById(list));
}
This code doesn't compile. "Required Flux<Race> but flatMap was inferred to Mono..."
Any idea on what I'm missing or how should I do?
Your problem is the collectList() part, since it makes a Mono from your Flux, which you obviously don't want - you're making a stream from the list immediately after collection. This here should do the same, plus mapping the Mono to a Flux with flatMapMany:
raceParticipationRepository.findByUserId(userId, status,
PageRequest.of(page, 6))
.map(ParticipationRepository::getRaceId)
.collectList(Collectors.toList()) // returns Mono<List>
.flatMapMany(list -> raceRepository.findAllById(list)); // Mono -> Flux again

Create temp values in AngularJS

In angular the following happened to me
function(){
$scope.varObj = {"name" : "john" , "age" : "20" };
$scope.tempVar1 = $scope.varObj;
$scope.tempVar2 = $scope.varObj ;
}
Now if i change tempVar2 automatically the value of tempVar1 is changed.
function(){
$scope.varObj = {"name" : "john" , "age" : "20" };
$scope.tempVar1 = $scope.varObj;
$scope.tempVar2 = $scope.varObj;
$scope.tempVar2.name = "mathews";
console.log($scope.tempVar1.name);
}
The output should be "john" because i am changing the value of tempVar2 not the tempVar1. But to the surprise the output is "mathews".
I tried both angular.copy and angular.extend but both produce the same output.
Can someone please help
I don't see any reason for it to not work with angular.copy
Heres' a fiddle with
demo
It's hapening becuase of two way binding
Try like this
$scope.tempVar1 = angular.copy($scope.varObj);
Use angular.copy() for it
By default only reference copy with assignment operator = which mean sharing the same memory space so if you change in any of the two object it will reflect into second one as well.
angular.copy() will create deep copy of the object
DOC:-
Creates a deep copy of source, which should be an object or an array.
$scope.varObj = {"name" : "john" , "age" : "20" };
$scope.tempVar1 = angular.copy($scope.varObj);
$scope.tempVar2 = angular.copy($scope.varObj);
$scope.tempVar2.name = "mathews";
console.log($scope.tempVar1.name);

MongoDB update multiple records of array [duplicate]

This question already has answers here:
How to Update Multiple Array Elements in mongodb
(16 answers)
Closed 5 years ago.
I recently started using MongoDB and I have a question regarding updating arrays in a document.
I got structure like this:
{
"_id" : ObjectId(),
"post" : "",
"comments" : [
{
"user" : "test",
"avatar" : "/static/avatars/asd.jpg",
"text" : "....."
}
{
"user" : "test",
"avatar" : "/static/avatars/asd.jpg",
"text" : "....."
}
{
"user" : "test",
"avatar" : "/static/avatars/asd.jpg",
"text" : "....."
}
...
]
}
I'm trying to execute the following query:
update({"comments.user":"test"},{$set:{"comments.$.avatar": "new_avatar.jpg"}},false,true)
The problem is that it update all documents, but it update only the first array element in every document. Is there any way to update all array elements or I should try to do it manually?
Thanks.
You cannot modify multiple array elements in a single update operation. Thus, you'll have to repeat the update in order to migrate documents which need multiple array elements to be modified. You can do this by iterating through each document in the collection, repeatedly applying an update with $elemMatch until the document has all of its relevant comments replaced, e.g.:
db.collection.find().forEach( function(doc) {
do {
db.collection.update({_id: doc._id,
comments:{$elemMatch:{user:"test",
avatar:{$ne:"new_avatar.jpg"}}}},
{$set:{"comments.$.avatar":"new_avatar.jpg"}});
} while (db.getPrevError().n != 0);
})
Note that if efficiency of this operation is a requirement for your application, you should normalize your schema such that the location of the user's avatar is stored in a single document, rather than in every comment.
One solution could be creating a function to be used with a forEach and evaling it (so it runs quickly). Assuming your collection is "article", you could run the following:
var runUpdate = function(){
db.article.find({"comments.user":"test").forEach( function(article) {
for(var i in article.comments){
article.comments[i].avatar = 'new_avatar.jpg';
}
db.article.save(article);
});
};
db.eval(runUpdate);
If you know the indexes you want to update you can do this with no problems like this:
var update = { $set: {} };
for (var i = 0; i < indexesToUpdate.length; ++i) {
update.$set[`comments.${indexesToUpdate[i]}. avatar`] = "new_avatar.jpg";
}
Comments.update({ "comments.user":"test" }, update, function(error) {
// ...
});
be aware that must of the IDE's will not accept the syntax but you can ignore it.
It seems like you can do this:
db.yourCollection.update({"comments.user":"test"},{$set:{"comments.0.avatar": "new_avatar.jpg", "comments.1.avatar": "new_avatar.jpg", etc...})
So if you have a small known number of array elements, this might be a little easier to do. If you want something like "comments.*.avatar" - not sure how to do that. It is probably not that good that you have so much data duplication tho..

Resources