The content of my JSON file is:
This is my retails.json file.actually retails is my database.i converted this json file from sql database.so in this json file, I want to delete a person information with a single ID.How can i do? i am using node.js and express.
{
"categories" : [
{
"dept_id" : "123",
"category_name" : "database",
"category_discription" : "list of database",
"current time" : "2016-07-21 06:27:17",
"id" : "1"
},
{
"dept_id" : "1234",
"category_name" : "debugging",
"category_discription" : "program debugger",
"current time" : "2016-07-21 06:32:24",
"id": "2"
},
{
"dept_id" : "12345",
"category_name" : "system analyzer",
"category_discription" : null,
"current time" : "2016-07-21 06:33:23",
"id" : "3"
}
],
"departments" : [
{
"name" : "manpreet singh",
"address_info" : "qadian",
"current time" : null,
"id" : "1"
},
{
"name" : "tushal gupta",
"address_info" : "amritsar",
"current time" : "2016-07-21 06:10:14",
"id" : "2"
},
{
"name" : "haroop singh",
"address_info" : "amritsar",
"current time" : "2016-07-21 06:11:12",
"id" : "3"
}
],
"digital_marketing" : [
{
"dept_id" : "123",
"phone" : "99889988",
"mobile" : null,
"email" : "thbs#gmail.com",
"web" : null,
"facebook" : null,
"twitter" : null,
"linkedin" : null,
"current time" : "2016-07-21 06:10:16",
"id" : "1"
},
{
"dept_id" : "1234",
"phone" : "998899888",
"mobile" : null,
"email" : null,
"web" : null,
"facebook" : "gtudgal#fb.com",
"twitter" : "tushalgupta",
"linkedin" : null,
"current time" : "2016-07-21 06:30:19",
"id" : "2"
},
{
"dept_id" : "12345",
"phone" : "99889877",
"mobile" : null,
"email" : "fhdts#mail.com",
"web" : null,
"facebook":"sdfh33#fb.com",
"twitter" : null,
"linkedin" : null,
"current time" : "2016-07-21 06:30:13",
"id" : "3"
}
]
}
I am using this to delete a ID, but does not work:
var id = 2;
app.get('/deleteUser', function (req, res) {
// First read existing users.
fs.readFile( __dirname + "/" + "retails.json", 'utf8', function (err, data) {
data = JSON.parse( data );
delete data["categories" + 2];
console.log( data );
res.end( JSON.stringify(data));
});
});
for (var prop in jsonObj.categories){
delete jsonObj.categories[prop].id;
}
similar for digital_marketing etc. jsonObj is the data in your case..
Related
I want to create a view from 2 different collections(info1, info2) in mongodb.
I need to have LoginId, FirstName,LastName,Email from info1 collection and GroupName,Type,MachName from info2 collection. Connecting field between info1 and info2 collections are "GroupName" and "Group" respectively
Below the collection content:
info1:
{
"GroupName" : "TEST Group1",
"LoginId" : "login1",
"FirstName" : "John",
"LastName" : "deo",
"Email" : "john.deo#xyz.com"
}
{
"GroupName" : "TEST Group2",
"LoginId" : "login1",
"FirstName" : "John",
"LastName" : "deo",
"Email" : "john.deo#xyz.com"
}
{
"GroupName" : "TEST Group2",
"LoginId" : "login2",
"FirstName" : "Mark",
"LastName" : "Clan",
"Email" : "mark.clan#xyz.com"
}
info2:
{
"Group" : "TEST Group1",
"Type" : "DEV",
"ActiveFlag" : "True",
"MachName" : "group1.xyz.net",
}
{
"Group" : "TEST Group2",
"Type" : "DEV",
"ActiveFlag" : "True",
"MachName" : "group2.xyz.net",
}
{
"Group" : "TEST Group1",
"Type" : "UAT",
"ActiveFlag" : "True",
"MachName" : "group1.xyz.net",
}
{
"Group" : "TEST Group2",
"Type" : "UAT",
"ActiveFlag" : "True",
"MachName" : "group2.xyz.net",
}
I want to have output as below from the view.
Expected output:
{
"GroupName" : ["TEST Group1", "TEST Group2"]
"LoginId" : "login1",
"FirstName" : "John",
"LastName" : "deo",
"Email" : "john.deo#xyz.com",
"Type" : "DEV",
"MachName" : ["group1.xyz.net","group2.xyz.net"]
},
{
"GroupName" : ["TEST Group1", "TEST Group2"],
"LoginId" : "login1",
"FirstName" : "John",
"LastName" : "deo",
"Email" : "john.deo#xyz.com",
"Type" : "UAT",
"MachName" : ["group1.xyz.net","group2.xyz.net"]
},
{
"GroupName" : ["TEST Group2"],
"LoginId" : "login2",
"FirstName" : "Mark",
"LastName" : "Clan",
"Email" : "mark.clan#xyz.com",
"Type" : "DEV",
"MachName" : ["group2.xyz.net"]
},
{
"GroupName" : ["TEST Group2"]
"LoginId" : "login2",
"FirstName" : "Mark",
"LastName" : "Clan",
"Email" : "mark.clan#xyz.com",
"Type" : "UAT",
"MachName" : ["group2.xyz.net"]
}
I have tried with below but not able to get the expected output. Can someone please help me to have view which should give the expected output from the 2 collections(info1,info2)?
Tried code(not working):
db.getCollection("info1").aggregate(
[
{
"$lookup" : {
"from" : "info2",
"localField" : "GroupName",
"foreignField" : "Group",
"as" : "g"
}
} ,
{ $match: { $and: [ {"g.ActiveFlag" : "True"} ] } },
{
"$project" : {
"LoginId" : "$LoginId",
"FirstName" : "$FirstName",
"LastName" : "$LastName",
"Email" : "$Email",
"machName" : "$g.MachName"
}
}
],
{
"allowDiskUse" : false
}
);
Demo - https://mongoplayground.net/p/SX3xH1v_2wQ
Use $group
$first
$push
Groups input documents by the specified _id expression and for each distinct grouping, outputs a document. The _id field of each output document contains the unique group by value. The output documents can also contain computed fields that hold the values of some accumulator expression.
db.info1.aggregate([
{
"$lookup": {
"from": "info2",
"localField": "GroupName",
"foreignField": "Group",
"as": "g"
}
},
{
$match: {
$and: [
{
"g.ActiveFlag": "True"
}
]
}
},
{
$group: {
_id: null, // you can goup by LoginId if you want
GroupName: { $push: "$GroupName" },
MachName: { $push: { $first: "$g.MachName" } },
LoginId: { $first: "$LoginId" },
FirstName: { $first: "$FirstName" },
Email: { $first: "$Email" },
Type: { $first: { $first: "$g.Type" } },
}
}
])
Update
Updated Demo - https://mongoplayground.net/p/0AgT3FJIB6P
Use $unwind on g after lookup pipeline
{ $unwind: "$g" }
Deconstructs an array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.
Demo - https://mongoplayground.net/p/IGzTzEbgfl0
$group: {
_id: {
Type: "$g.Type",
LoginId: "$LoginId"
}
// ....
}
My very first question after using this site for my own learning! Still a beginner so go easy on me :)
I am trying to format JSON data within MSSQL Server. I have a static JSON file which I can get to display via OPENROWSET, and populate a variable. This JSON file has a "header" and then one or more "child" rows, basically an order header and order detail lines. I can successfully separately display the header's columns as a table in a result-set. I'd like to do the same with just the detail lines - the aim being to then store the header in a table and it's details in a table within SQL server - this part I'll have no issue with.
Here is some mock-up JSON data that I'm working with. This is the exact format I need to use, so I don't have any room to manoeuvre with it but I've populated it with test data:
{
"InputParameters" : {
"P_IN_ORDER_SOURCE" : "The_Web",
"P_IN_ORIG_SYS_DOCUMENT_REF" : "Order666",
"P_IN_SOLD_TO_CUST_NUMBER" : "JOEB11",
"P_IN_CUST_ORDER_NUMBER" : "JoeB5556667",
"P_IN_REQUEST_DATE" : "2021-01-20 08:10:06",
"P_IN_ORDER_ENTRY_DATE" : "2021-01-20 08:10:06",
"P_IN_SHIPTO_NAME" : "The Testing Co.",
"P_IN_SHIPTO_ADDR" : "82 Annweir Crescent",
"P_IN_SHIPTO_ADDR_2" : null,
"P_IN_SHIPTO_CITY" : "Atlantis",
"P_IN_SHIPTO_STATE" : "WSX",
"P_IN_SHIPTO_ZIP" : "AT55 666",
"P_IN_SHIPTO_COUNTRY" : "GB",
"P_IN_OPERATION_CODE" : "CREATE",
"P_IN_BOOKED_FLAG" : "N",
"P_IN_OU_NAME" : "ATL UK OU",
"P_IN_SPECIAL_INSTRUCTIONS" : null,
"P_IN_QUOTE_NUMBER" : null,
"P_IN_PRICELIST_ID" : "8",
"P_IN_EMAIL" : "testemail#testemail.com",
"P_IN_SHIPTO_COUNTY" : null,
"P_IN_SHIPPING_METHOD" : "Pre 930",
"P_IN_SHIPPING_INSTRUCTIONS" : null,
"P_IN_ATTENTION_TO" : "Joe Bloggs",
"P_IN_FREIGHT_CARRIER_CODE" : null,
"P_IN_IS_RETURN" : null,
"P_IN_SALES_REP" : null,
"P_IN_LINE_DATA" : [ {
"P_IN_LINE_DATA_ITEM" : [ {
"ORIG_SYS_DOCUMENT_REF" : "Order666",
"ORIG_SYS_LINE_REF" : "1",
"CUSTOMER_LINE_NUMBER" : "1",
"ITEM_TYPE_CODE" : "STANDARD",
"ITEM_DESCRIPTION" : "SKU7776",
"USER_ITEM_DESCRIPTION" : "SKU7776",
"TOP_MODEL_LINE_REF" : null,
"LINK_TO_LINE_REF" : null,
"COMPONENT_CODE" : null,
"ORDERED_QUANTITY" : "6",
"ORDER_QUANTITY_UOM" : null,
"UNIT_LIST_PRICE" : "16.95",
"UNIT_SELLING_PRICE" : "16.95",
"CALCULATE_PRICE_FLAG" : "N",
"OPERATION_CODE" : "INSERT"
}, {
"ORIG_SYS_DOCUMENT_REF" : "Order666",
"ORIG_SYS_LINE_REF" : "2",
"CUSTOMER_LINE_NUMBER" : "2",
"ITEM_TYPE_CODE" : "STANDARD",
"ITEM_DESCRIPTION" : "SKU12345",
"USER_ITEM_DESCRIPTION" : "SKU12345",
"TOP_MODEL_LINE_REF" : null,
"LINK_TO_LINE_REF" : null,
"COMPONENT_CODE" : null,
"ORDERED_QUANTITY" : "6",
"ORDER_QUANTITY_UOM" : null,
"UNIT_LIST_PRICE" : "11.89",
"UNIT_SELLING_PRICE" : "11.89",
"CALCULATE_PRICE_FLAG" : "N",
"OPERATION_CODE" : "INSERT"
}, {
"ORIG_SYS_DOCUMENT_REF" : "Order666",
"ORIG_SYS_LINE_REF" : "3",
"CUSTOMER_LINE_NUMBER" : "3",
"ITEM_TYPE_CODE" : "STANDARD",
"ITEM_DESCRIPTION" : "SKU9999",
"USER_ITEM_DESCRIPTION" : "SKU9999",
"TOP_MODEL_LINE_REF" : null,
"LINK_TO_LINE_REF" : null,
"COMPONENT_CODE" : null,
"ORDERED_QUANTITY" : "8",
"ORDER_QUANTITY_UOM" : null,
"UNIT_LIST_PRICE" : "46.42",
"UNIT_SELLING_PRICE" : "46.42",
"CALCULATE_PRICE_FLAG" : "N",
"OPERATION_CODE" : "INSERT"
} ]
} ]
}
}
I've been trying to learn how to use this JSON with SQL server pretty much starting from today. I've explored the OPENJSON() function which like I've said, I can define the separate columns and path with the header information - but as soon as I try to do similar and path to the detail objects, I just get NULLs back in each column.
Any suggestions at all? Apologies if I've missed any key information out here! Many thanks!
Something like this:
declare #json nvarchar(max) = '
{
"InputParameters" : {
"P_IN_ORDER_SOURCE" : "The_Web",
"P_IN_ORIG_SYS_DOCUMENT_REF" : "Order666",
"P_IN_SOLD_TO_CUST_NUMBER" : "JOEB11",
"P_IN_CUST_ORDER_NUMBER" : "JoeB5556667",
"P_IN_REQUEST_DATE" : "2021-01-20 08:10:06",
"P_IN_ORDER_ENTRY_DATE" : "2021-01-20 08:10:06",
"P_IN_SHIPTO_NAME" : "The Testing Co.",
"P_IN_SHIPTO_ADDR" : "82 Annweir Crescent",
"P_IN_SHIPTO_ADDR_2" : null,
"P_IN_SHIPTO_CITY" : "Atlantis",
"P_IN_SHIPTO_STATE" : "WSX",
"P_IN_SHIPTO_ZIP" : "AT55 666",
"P_IN_SHIPTO_COUNTRY" : "GB",
"P_IN_OPERATION_CODE" : "CREATE",
"P_IN_BOOKED_FLAG" : "N",
"P_IN_OU_NAME" : "ATL UK OU",
"P_IN_SPECIAL_INSTRUCTIONS" : null,
"P_IN_QUOTE_NUMBER" : null,
"P_IN_PRICELIST_ID" : "8",
"P_IN_EMAIL" : "testemail#testemail.com",
"P_IN_SHIPTO_COUNTY" : null,
"P_IN_SHIPPING_METHOD" : "Pre 930",
"P_IN_SHIPPING_INSTRUCTIONS" : null,
"P_IN_ATTENTION_TO" : "Joe Bloggs",
"P_IN_FREIGHT_CARRIER_CODE" : null,
"P_IN_IS_RETURN" : null,
"P_IN_SALES_REP" : null,
"P_IN_LINE_DATA" : [ {
"P_IN_LINE_DATA_ITEM" : [ {
"ORIG_SYS_DOCUMENT_REF" : "Order666",
"ORIG_SYS_LINE_REF" : "1",
"CUSTOMER_LINE_NUMBER" : "1",
"ITEM_TYPE_CODE" : "STANDARD",
"ITEM_DESCRIPTION" : "SKU7776",
"USER_ITEM_DESCRIPTION" : "SKU7776",
"TOP_MODEL_LINE_REF" : null,
"LINK_TO_LINE_REF" : null,
"COMPONENT_CODE" : null,
"ORDERED_QUANTITY" : "6",
"ORDER_QUANTITY_UOM" : null,
"UNIT_LIST_PRICE" : "16.95",
"UNIT_SELLING_PRICE" : "16.95",
"CALCULATE_PRICE_FLAG" : "N",
"OPERATION_CODE" : "INSERT"
}, {
"ORIG_SYS_DOCUMENT_REF" : "Order666",
"ORIG_SYS_LINE_REF" : "2",
"CUSTOMER_LINE_NUMBER" : "2",
"ITEM_TYPE_CODE" : "STANDARD",
"ITEM_DESCRIPTION" : "SKU12345",
"USER_ITEM_DESCRIPTION" : "SKU12345",
"TOP_MODEL_LINE_REF" : null,
"LINK_TO_LINE_REF" : null,
"COMPONENT_CODE" : null,
"ORDERED_QUANTITY" : "6",
"ORDER_QUANTITY_UOM" : null,
"UNIT_LIST_PRICE" : "11.89",
"UNIT_SELLING_PRICE" : "11.89",
"CALCULATE_PRICE_FLAG" : "N",
"OPERATION_CODE" : "INSERT"
}, {
"ORIG_SYS_DOCUMENT_REF" : "Order666",
"ORIG_SYS_LINE_REF" : "3",
"CUSTOMER_LINE_NUMBER" : "3",
"ITEM_TYPE_CODE" : "STANDARD",
"ITEM_DESCRIPTION" : "SKU9999",
"USER_ITEM_DESCRIPTION" : "SKU9999",
"TOP_MODEL_LINE_REF" : null,
"LINK_TO_LINE_REF" : null,
"COMPONENT_CODE" : null,
"ORDERED_QUANTITY" : "8",
"ORDER_QUANTITY_UOM" : null,
"UNIT_LIST_PRICE" : "46.42",
"UNIT_SELLING_PRICE" : "46.42",
"CALCULATE_PRICE_FLAG" : "N",
"OPERATION_CODE" : "INSERT"
} ]
} ]
}
}
'
select *
from openjson(#json,'$.InputParameters.P_IN_LINE_DATA[0].P_IN_LINE_DATA_ITEM')
with
(
ORIG_SYS_DOCUMENT_REF varchar(200),
ORIG_SYS_LINE_REF int,
CUSTOMER_LINE_NUMBER int,
ITEM_TYPE_CODE varchar(200),
-- . . .
OPERATION_CODE varchar(200)
)
Hello guys I'm new to angularjs.I'm trying to apply filter on the following thing.But what happening is it is not iterating all list values with ng-repeat followed by some filters.It only iterating jobseekerId 1,2 only but not 3.Where as if I'm removing the filter means it iterating all list values(1,2,3).
What is the problem with my code .Please give some suggestion.
This is my data
[ {
"jobSeekerId" : 1,
"firstName" : "vijay",
"middleName" : null,
"lastName" : "sury",
"jobSeekerProfTitle" : "Having 2.2yerars of experience in the field java development with spring and hibernate",
"profileUpdateDate" : "05 May 2014",
"currentState" : "ap",
"profilePath" : "E:/opt/jsimages/recru-profile.jpg",
"currentCity" : "hyd",
"salary" : "20000",
"salaryType" : "per month",
"jobSeekerSkillVo" : [ {
"skill" : "js"
}, {
"skill" : "java"
}, {
"skill" : "uuu"
} ],
"academicInfoVo" : [ {
"academicInfoId" : 1,
"courseName" : "btech",
"universityName" : "intuk",
"academicEndYear" : 2014,
"gpa" : 10.0
} ],
"city" : [ "hyd", "ts" ],
"totalExperience" : "48",
"viewCount" : "2",
"downloadCount" : "0",
"viewStatus" : "alive"
}, {
"jobSeekerId" : 2,
"firstName" : "raj",
"middleName" : null,
"lastName" : "sury",
"jobSeekerProfTitle" : null,
"profileUpdateDate" : null,
"currentState" : null,
"profilePath" : null,
"currentCity" : null,
"salary" : null,
"salaryType" : null,
"jobSeekerSkillVo" : [ {
"skill" : "uuu"
}, {
"skill" : "java"
} ],
"academicInfoVo" : [ {
"academicInfoId" : 2,
"courseName" : "btech",
"universityName" : "intuk",
"academicEndYear" : 2014,
"gpa" : 8.0
} ],
"city" : [ ],
"totalExperience" : "14",
"viewCount" : "1",
"downloadCount" : "0",
"viewStatus" : "alive"
}, {
"jobSeekerId" : 3,
"firstName" : "vj",
"middleName" : null,
"lastName" : "gg",
"jobSeekerProfTitle" : null,
"profileUpdateDate" : null,
"currentState" : null,
"profilePath" : null,
"currentCity" : null,
"salary" : "20000",
"salaryType" : "per month",
"jobSeekerSkillVo" : [ ],
"academicInfoVo" : [ ],
"city" : [ ],
"totalExperience" : "0",
"viewCount" : "1",
"downloadCount" : "0",
"viewStatus" : "alive"
} ]
======================
This is my filter
<div class="wrapper" ng-app="candidateListApp" ng-controller="candidateListController">
<span ng-cloak ng-repeat="jsList in jobSeekerList|filter:{jobSeekerSkillVo:
{skill:drpSkill},})}}">
<a ng-href="#">
{{jsList.firstName}} {{jsList.lastName}}
</a>
</span>
<span ng-if="jsList.jobSeekerSkillVo.length != 0">
<span ng-repeat="skills in jsList.jobSeekerSkillVo">
{{skills.skill}}
{{$last?'':','}}
</span>
</span>
</div>
<script>
var app=angular.module("candidateListApp",[]);
app.controller("candidateListController",function($scope,$http){
$scope.jobSeekerList=${jobSeekerJsonArray};
});
</script>
This is my model (with jsp code)
<select id="skills" name="skills" class="form-control2" ng-model="drpSkill">
<option value="" disabled="disabled" selected="selected">Skills</option>
<c:forEach items="${skillVo}" var="skills">
<option value="${skills.skill}">${skills.skill}</option>
</c:forEach>
</select>
I have to convert a JSON file in a CSV file or even better, extract the JSON file content in to an Oracle table.
I gave a look into the other topics and the only one that I found is related to the conversion in HTML table.
The files are really unstructured, here an example:
{
"message-version" : "1.2",
"orcid-profile" : {
"orcid" : null,
"orcid-id" : null,
"orcid-identifier" : {
"value" : null,
"uri" : "http://orcid.org/0000-0002-3285-9536",
"path" : "0000-0002-3285-9536",
"host" : "orcid.org"
},
"orcid-deprecated" : null,
"orcid-preferences" : {
"locale" : "EN"
},
"orcid-history" : {
"creation-method" : "MEMBER_REFERRED",
"completion-date" : null,
"submission-date" : {
"value" : 1417193651961
},
"last-modified-date" : {
"value" : 1418131422749
},
"claimed" : {
"value" : true
},
"source" : null,
"deactivation-date" : null,
"verified-email" : {
"value" : true
},
"verified-primary-email" : {
"value" : true
},
"visibility" : null
},
"orcid-bio" : {
"personal-details" : {
"given-names" : {
"value" : "Marcin"
},
"family-name" : {
"value" : "Serocki"
},
"credit-name" : null,
"other-names" : null
},
"biography" : null,
"researcher-urls" : null,
"contact-details" : null,
"keywords" : null,
"external-identifiers" : null,
"delegation" : null,
"applications" : null,
"scope" : null
},
"orcid-activities" : {
"affiliations" : {
"affiliation" : [ {
"type" : "EDUCATION",
"department-name" : "Department of Pharmaceutical Technology and Biochemistry",
"role-title" : "PhD",
"start-date" : {
"year" : {
"value" : "2010"
},
"month" : {
"value" : "09"
},
"day" : {
"value" : "01"
}
},
"end-date" : null,
"organization" : {
"name" : "Gdansk University of Technology",
"address" : {
"city" : "Gdansk",
"region" : null,
"country" : "PL"
},
"disambiguated-organization" : null
},
"source" : {
"source-orcid" : {
"value" : null,
"uri" : "http://orcid.org/0000-0002-3285-9536",
"path" : "0000-0002-3285-9536",
"host" : "orcid.org"
},
"source-client-id" : null,
"source-name" : {
"value" : "Marcin Serocki"
},
"source-date" : {
"value" : 1418131402196
}
},
"created-date" : {
"value" : 1418131402196
},
"last-modified-date" : {
"value" : 1418131402196
},
"visibility" : "PUBLIC",
"put-code" : "611828"
}, {
"type" : "EDUCATION",
"department-name" : "Department of Pharmaceutical Technology and Biochemistry",
"role-title" : "MSc",
"start-date" : {
"year" : {
"value" : "2005"
},
"month" : {
"value" : "09"
},
"day" : {
"value" : "01"
}
},
"end-date" : {
"year" : {
"value" : "2010"
},
"month" : {
"value" : "07"
},
"day" : {
"value" : "01"
}
},
"organization" : {
"name" : "Gdansk University of Technology",
"address" : {
"city" : "Gdansk",
"region" : null,
"country" : "PL"
},
"disambiguated-organization" : null
},
"source" : {
"source-orcid" : {
"value" : null,
"uri" : "http://orcid.org/0000-0002-3285-9536",
"path" : "0000-0002-3285-9536",
"host" : "orcid.org"
},
"source-client-id" : null,
"source-name" : {
"value" : "Marcin Serocki"
},
"source-date" : {
"value" : 1418131329327
}
},
"created-date" : {
"value" : 1418131329327
},
"last-modified-date" : {
"value" : 1418131329327
},
"visibility" : "PUBLIC",
"put-code" : "611826"
} ]
},
"orcid-works" : null,
"funding-list" : null
},
"orcid-internal" : null,
"type" : "USER",
"group-type" : null,
"client-type" : null
},
"orcid-search-results" : null,
"error-desc" : null
}
I have no idea how to proceed?
All code is available at github.
The code is updated according to the changed did in csv in 2013 since then CSVs now have headers! The key name for the field is now the first row of the CSV.
https://github.com/vinay20045/json-to-csv
OR
You can use api
https://json-csv.com/api to convert.
Just register there get api accessibility privileges and use thier code. That's it
I want update a array value that is nested within an array value: i.e. set
status = enabled
where alerts.id = 2
{
"_id" : ObjectId("5496a8ed49847b6cd7c7b350"),
"name" : "joe",
"locations" : [
{
"name": "my location",
"alerts" : [
{
"id" : 1,
"status" : null
},
{
"id" : 2,
"status" : null
}
]
}
]
}
I would have used the position $ character, but cannot use it twice in a statement - multi positional operators are not supported yet: https://jira.mongodb.org/browse/SERVER-831
How do I issue a statement to only update the status field of an alert matching an id of 2?
UPDATE
If I change the schema as follows:
{
"_id" : ObjectId("5496ab2149847b6cd7c7b352"),
"name" : "joe",
"locations" : {
"my location" : {
"alerts" : [
{
"id" : 1,
"status" : "enabled"
},
{
"id" : 2,
"status" : "enabled"
}
]
},
"my other location" : {
"alerts" : [
{
"id" : 3,
"status" : null
},
{
"id" : 4,
"status" : null
}
]
}
}
}
I can then use:
update({"locations.my location.alerts.id":1},{$set: {"locations.my location.alerts.$.status": "enabled"}});
Problem is I cannot create indexes on the alert id :-(
it may be better of modelled as such, specially if an index on location and,or alerts.id is needed.
{
"_id" : ObjectId("5496a8ed49847b6cd7c7b350"),
"name" : "joe",
"location" : "myLocation",
"alerts" : [{
"id" : 1,
"status" : null
},
{
"id" : 2,
"status" : null
}
]
}
{
"_id" : ObjectId("5496a8ed49847b6cd7c7b350"),
"name" : "joe",
"location" : "otherLocation",
"alerts" : [{
"id" : 1,
"status" : null
},
{
"id" : 2,
"status" : null
}
]
}
I think you are having a wrong tool for the job. What you have in your example is relational data and it's much easier to handle with relational database. So I would suggest to use SQL-database instead of mongo.
But if you really want to do it with mongo, then I guess the only option is to fetch the document and modify it and put it back.