//Existing Json that i have
[
{
"Id":1,
"Authors":[
{
"Id":10,
"Name":"Arun"
},
{
"Id":14,
"Name":"Arjun"
}
],
"tags":[
{
"Name":"Java"
}
]
},
{
"Id":5,
"Authors":[
{
"Id":7,
"Name":"Shyama"
}
],
"tags":[
{
"Name":"C#"
},
{
"Name":"C++"
}
]
}
]
//New Json (converted)
//Here i want to combine both the author
[
{
"Id":1,
"Authors":"Arun, Arjun",
"tags":"Java"
},
{
"Id":5,
"Authors":"Shyama",
"tags":"C#,C++"
}
]
//Controller that i have created
$scope.request = response.data;//request contain all json data
//---------- logic to create new json------------
}
var json = [
{
"Id":1,
"Authors":[
{
"Id":10,
"Name":"Arun"
},
{
"Id":14,
"Name":"Arjun"
}
],
"tags":[
{
"Name":"Java"
}
]
},
{
"Id":5,
"Authors":[
{
"Id":7,
"Name":"Shyama"
}
],
"tags":[
{
"Name":"C#"
},
{
"Name":"C++"
}
]
}
];
for (var i=0, j=json.length; i<j; i++) {
json[i].Authors = json[i].Authors.map(function(item) {return item.Name;}).join(',');
json[i].tags = json[i].tags.map(function(item) {return item.Name;}).join(',');
}
console.log(json);
Related
I am using includeArrayIndex to add the rank based on sorting
Here is the aggregation query
dbo.collection("funds").aggregate([
{
"$sort": {
"amount": -1
}
},
{
"$group": {
"_id": "",
"items": {
"$push": "$$ROOT"
}
}
},
{
"$unwind": {
"path": "$items",
"includeArrayIndex": "items.rank"
}
},
{
"$replaceRoot": {
"newRoot": "$items"
}
},
{
"$sort": {
"rank": -1
}
}
])
and this assign the rank field but starting from 0, is there any way to start it from 1
tried with set like this
[
{
'$sort': {
'amount': -1
}
}, {
'$group': {
'_id': '',
'items': {
'$push': '$$ROOT'
}
}
}, {
'$unwind': {
'path': '$items',
'includeArrayIndex': 'items.rank'
}
}, {
'$replaceRoot': {
'newRoot': '$items'
}
},
{
'$set': {
'rank': {
'$add': [
'$rank', 1
]
}
}
},
// {
// '$sort': {
// 'rank': 1
// }
// }
]
in the mongodb compass aggregation tab it shows that field got added but when i run this script using nodejs it does not add the rank field
even i tried with
const a = await dbo.collection("funds").aggregate(
[
{
'$sort': {
'amount': 1
}
}, {
'$group': {
'_id': '',
'items': {
'$push': '$$ROOT'
}
}
}, {
'$unwind': {
'path': '$items',
'includeArrayIndex': 'items.rank'
}
}, {
'$replaceRoot': {
'newRoot': '$items'
}
},
{
'$addFields': {
'rank': {
'$add': [
'$rank', 1
]
}
}
}
]
).toArray();
and this even prints on console as a
[
{ _id: new ObjectId("6220d2fe20e33d48c865b720"), amount: 1, rank: 1 },
{
_id: new ObjectId("6220d2cf20e33d48c865b71e"),
amount: 10,
rank: 2
},
{
_id: new ObjectId("6220d2f520e33d48c865b71f"),
amount: 12,
rank: 3
}
]
and then tried with $setWindowFields
dbo.collection("funds").aggregate( [
{
$setWindowFields: {
sortBy: { amount: -1 },
output: {
rank: {
$rank: {}
}
}
}
}
] )
but it shows
err MongoServerError: Unrecognized pipeline stage name: '$setWindowFields'
Sample document is like
[
{amount : 20, name :""},
{amount : 22, name :""}
]
Not sure if this was already answered. But you just have to add { $out: "funds" }
at the last of your code. PFB the working code ...
const a = await dbo.collection("funds").aggregate(
[
{
'$sort': {
'amount': 1
}
}, {
'$group': {
'_id': '',
'items': {
'$push': '$$ROOT'
}
}
}, {
'$unwind': {
'path': '$items',
'includeArrayIndex': 'items.rank'
}
}, {
'$replaceRoot': {
'newRoot': '$items'
}
},
{
'$addFields': {
'rank': {
'$add': [
'$rank', 1
]
}
}
},
{ $out: "funds" }
], {allowDiskUse:true}
).toArray();
In my MongoDB database, I have a collection 'produits' with documents like this
{
"_id": {
"$oid": "6048e97b4a5f000096007505"
},
"modeles": [
{
"id": "OppoA3",
"pieces": [
{
"id": "OppoA3avn"
},
{
"id": "OppoA3bat"
}]
]
},
{
"id": "OppoA1",
"pieces": [
{
"id": "OppoA1avn",
},
{
"id": "OppoA1batt",
}
]
}
]
}
How can I delete all modeles.pieces from all my documents.
I managed to delete with a filter on modeles.id but with that code but not on all the collection
db.produits.update(
{marque_id:'OPPO', 'modeles.id':'RENOZ'},
{$set:
{
'modeles.$.pieces': []
}
}
, { multi : true }
)
I would like all documents like this finally
{
"_id": {
"$oid": "6048e97b4a5f000096007505"
},
"modeles": [
{
"id": "OppoA3",
"pieces": []
},
{
"id": "OppoA1",
"pieces": []
}
]
}
Thank you for your help.
I have done a javascript loop like this, but i think it's not best practice
async removePieces(){
var doc
try {
doc = await produitModel.find()
for (var produit of doc) {
for (var modele of produit.modeles) {
const filter = {'marque_id': produit.marque_id, 'modeles.id': modele.id}
const set = {
$set: {
'modeles.$.pieces': []
}
}
await db.collection('produits').updateOne(filter, set)
}
}
console.log('removePieces() ==> Terminé')
} catch(err) {
console.log(err)
}
}
db.produits.update({
modeles: {//This is because your second document will create failure otherwise
$exists: true
}
},
{
$set: {
"modeles.$.pieces": []
}
},
{
multi: true
})
Calling an api and getting the response in xml. I am trying to convert the xml output to JSON using xml2json. How can now i read directly the temperature parameter value from the json output.
I am using following in my code,
var xml = response.body;
var result = convert.xml2json(xml, {compact: true, spaces: 4});
const jsonData = JSON.parse(result);
res.send(jsonData);
jsonData has all data but i just want to read e.g temperature value
Here is the json output after conversion,
{
"_declaration": {
"_attributes": {
"version": "1.0",
"encoding": "UTF-8"
}
},
"wfs:FeatureCollection": {
"_attributes": {
"timeStamp": "2019-05-02T17:21:05Z",
"numberReturned": "864",
"numberMatched": "864",
"xmlns:wfs": "http://www.opengis.net/wfs/2.0",
"xmlns:gml": "http://www.opengis.net/gml/3.2",
"xmlns:BsWfs": "http://xml.fmi.fi/schema/wfs/2.0",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xsi:schemaLocation": "http://www.opengis.net/wfs/2.0
http://schemas.opengis.net/wfs/2.0/wfs.xsd\n
http://xml.fmi.fi/schema/wfs/2.0
http://xml.fmi.fi/schema/wfs/2.0/fmi_wfs_simplefeature.xsd"
},
"wfs:member": [
{
"BsWfs:BsWfsElement": {
"_attributes": {
"gml:id": "BsWfsElement.1.1.1"
},
"BsWfs:Location": {
"gml:Point": {
"_attributes": {
"gml:id": "BsWfsElementP.1.1.1",
"srsDimension": "2",
"srsName": "http://www.opengis.net/def/crs/EPSG/0/4258"
},
"gml:pos": {
"_text": "60.20520 24.65220 "
}
}
},
"BsWfs:Time": {
"_text": "2019-05-02T18:00:00Z"
},
"BsWfs:ParameterName": {
"_text": "GeopHeight"
},
"BsWfs:ParameterValue": {
"_text": "36.57"
}
}
},
{
"BsWfs:BsWfsElement": {
"_attributes": {
"gml:id": "BsWfsElement.1.1.2"
},
"BsWfs:Location": {
"gml:Point": {
"_attributes": {
"gml:id": "BsWfsElementP.1.1.2",
"srsDimension": "2",
"srsName":
"http://www.opengis.net/def/crs/EPSG/0/4258"
},
"gml:pos": {
"_text": "60.20520 24.65220 "
}
}
},
"BsWfs:Time": {
"_text": "2019-05-02T18:00:00Z"
},
"BsWfs:ParameterName": {
"_text": "Temperature"
},
"BsWfs:ParameterValue": {
"_text": "2.97"
}
}
}
]
}
}
jsonData['wfs:FeatureCollection']['wfs:member'][0]['BsWfs:BsWfsElement']['BsWfs:ParameterName']['_text']
ex)
jsonData = { "key0": 34 }
jsonArray = [ 35,36,37 ]
=>
console.log(jsonData['key0"]); // 34
console.log(jsonArray[0]); // 35
I am new in groovy and I want to construct a json object with the builder
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{ "match": { "content": "scontent" } },
{ "match": { "title":"stitle" } }
]
}
},
{
"bool": {
"should": [
{ "match": { "a1": "v1" } },
{ "match": { "a2":"v2" } },
... and so on ...
{ "match": { "an":"vn" } }
]
}
}
]
}
},
"highlight": {
"fields": {
"content":{}
}
}
}
I search a lot of on other posts on stackoverflow and I write this code
So I did this but no way to get what I want :
JsonBuilder builder = new JsonBuilder()
def body = builder {
from Lib.or(qQuery.start, 0)
size Lib.or(qQuery.num, 10)
query {
bool {
must [
{
bool {
should [
{ match { content 'scontent' } },
{ match { title 'stitle' } }
]
}
},
{
bool {
should myVals.collect {[
'match' : { it.key it.value }
]}
}
}
]
}
}
highlight {
fields {
content {}
}
}
}
Thanks for any help !
I think you can make this work with the JsonBuilder as is, but it is usually easier to create the data structure using maps and lists (which is what the builder outputs) in groovy as you have more control there.
Example code:
import groovy.json.*
def data = [
query: [
bool: [
must: [
[bool:
[should: [
[match: [ content: 'scontent']],
[match: [ title: 'stitle']]
]]
],
[bool:
[should: [
[match: [ a1: 'v1']],
[match: [ a2: 'v2']],
[match: [ vn: 'vn']]
]]
]
]
]
]
]
println JsonOutput.prettyPrint(JsonOutput.toJson(data))
produces:
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"match": {
"content": "scontent"
}
},
{
"match": {
"title": "stitle"
}
}
]
}
},
{
"bool": {
"should": [
{
"match": {
"a1": "v1"
}
},
{
"match": {
"a2": "v2"
}
},
{
"match": {
"vn": "vn"
}
}
]
}
}
]
}
}
}
I did not include your full json as it takes up some space, but the structure is there. Note the use of lists ([valueA, valueB]) vs maps ([someKey: someValue]) in the data structure.
Granted this makes the JsonBuilder less than 100% useful but I haven't seen any concise ways of including lists of large json objects in a list within the structure. You can do:
def json = JsonBuilder()
json.query {
bool('list', 'of', 'values')
}
but for larger structures as list elements I would say go with the lists and maps approach.
i have the following json file. i am unable to get the data dynamically. please help me to solve this issue.
my requirement is i need to view the url value based on the submodule ie; sname || id. if i am accessing the sname as chapter1 i need to get all the vid -> url values that are present for that sub module.
[
{
"name":"Introduction - About The PMP Credential",
"sub1":[
{
"sname":"Introduction to PMP",
"id":"1",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Chapter1",
"id":"2",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Justification",
"id":"3",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
}
]
},
{
"name":"Project Management Framework",
"sub1":[
{
"sname":"Project Seminar",
"id":"4",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Introduction to Communication Management",
"id":"5",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Final Communication Management",
"id":"6",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Modules",
"id":"7",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
}
]
},
{
"name":"Cost Management",
"sub1":[
{
"sname":"Introduction to Cost Management",
"id":"8",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Cost Management Introduction",
"id":"9",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
}
]
}
]
<div ng-repeat="item_nm in test">{{item_nm.name}}
<div ng-repeat="sub1_dt in item_nm.sub1">
{{sub1_dt.sname}}
<div ng-repeat="vid in sub1_dt.vid">
{{vid.url}}
</div>
</div>
</div>
This will display name and corresponding sname and its url.