i have a document in mongodb like this:
db.full_stock_order_flow.find({skuId:"a19011100abc0084",stockGroup:"ABC_01"})
result:
{
"_id" : ObjectId("59bb5698c37fe6085b36f7d5"),
"skuId" : "a19011100abc0084",
"stockGroup" : "ABC_01",
"orderflowJsonEncode" : [
{
"calcDate" : "2016-01-28",
"acbQty" : NumberInt(0),
"abcQty" : NumberInt(30),
"stockQty" : NumberInt(0),
"isActiveDay" : true,
"outQtyWhenFullStockQty" : NumberInt(30)
},
{
"calcDate" : "2016-01-29",
"acbQty" : NumberInt(0),
"abcQty" : NumberInt(13),
"stockQty" : NumberInt(53),
"isActiveDay" : true,
"outQtyWhenFullStockQty" : NumberInt(13)
}
],
"createdOn" : "2017-09-05 12:27:04",
"createdBy" : "helloworld"
}
and i want to find the sub document and insert it the other collectin with parent Document like change 2 document without sub document in the other document:
{
"_id" : ObjectId("59bb5698c37kk6085b36f7d5"),
"skuId" : "a19011100abc0084",
"stockGroup" : "ABC_01",
"calcDate" : "2016-01-28",
"acbQty" : NumberInt(0),
"abcQty" : NumberInt(30),
"stockQty" : NumberInt(0),
"isActiveDay" : true,
"outQtyWhenFullStockQty" : NumberInt(30),
"createdOn" : "2017-09-05 12:27:04",
"createdBy" : "helloworld"
}
{
"_id" : ObjectId("59bb5698c37kk6085b36f7d5"),
"skuId" : "a19011100abc0084",
"stockGroup" : "ABC_01",
"calcDate" : "2016-01-29",
"acbQty" : NumberInt(0),
"abcQty" : NumberInt(13),
"stockQty" : NumberInt(53),
"isActiveDay" : true,
"outQtyWhenFullStockQty" : NumberInt(13),
"createdOn" : "2017-09-05 12:27:04",
"createdBy" : "helloworld"
}
what can i do?
i use this :
var record=db.full_stock_order_flow.find({skuId:"a19011100abc0084",stockGroup:"US_01"});
var arr=record.orderflowJsonEncode;
arr.forEach(
function(item){
db.tt123.insert({"skuId": record.skuId,"stockGroup": record.stockGroup , "orderflowJsonEncode":item});
}
)
Related
We are on Oracle 11g, no chance of upgrade at this time.
This JSON comes from an outside source, no chance of reformat.
Is this JSON the problem? I am able to run htis code using simple JSON.
APEX_JSON.get_count only seems to recognize the path hits.hits
I need to create an inner loop to parse full_na
declare
l_cnt PLS_INTEGER ;
l_cnt_2 PLS_INTEGER ;
json varchar2 (32767)
:= '{
"took" : 45,
"timed_out" : false,
"_shards" : {
"total" : 102,
"successful" : 102,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.0,
"hits" : [
{
"_index" : "pprepv5",
"_type" : "_doc",
"_id" : "pty990183115",
"_score" : 0.0,
"_routing" : "16",
"_source" : {
"pty_id" : 990183115,
"asgmt_typ_cde" : null,
"country_code" : null,
"decd_ind" : "N",
"pay_dir_ind" : "N",
"paybl_ind" : null,
"paybl_soc_cde" : null,
"paybl_soc_pty_id" : null,
"pty_typ_cde" : "NFMBR",
"succr_act_fl" : null,
"txfr_pty_id" : null,
"del_fl" : "N",
"ipi_bas_nr" : "I-001775403-7",
"pty_rol_typ_cde" : "W",
"document_type" : "entity",
"entity_type" : "pty",
"my_join_field" : "entity"
},
"inner_hits" : {
"name" : {
"hits" : {
"total" : 3,
"max_score" : 10.105857,
"hits" : [
{
"_index" : "pprepv5",
"_type" : "_doc",
"_id" : "name11627766990183115",
"_score" : 10.105857,
"_routing" : "16",
"_source" : {
"pty_na_id" : 1.1627766E7,
"del_fl" : "N",
"suf" : null,
"pty_id" : 990183115,
"na" : "LAMBERT",
"fst_na" : "MIRANDA",
"sal" : null,
"mid_init" : null,
"pty_na_typ_cde" : "PP",
"ipi_na_nr" : 4.00943E8,
"full_na" : "LAMBERT MIRANDA",
"suf_oth" : null,
"my_join_field" : {
"name" : "name",
"parent" : "pty990183115"
},
"document_type" : "name"
}
},
{
"_index" : "pprepv5",
"_type" : "_doc",
"_id" : "name11627765990183115",
"_score" : 8.721075,
"_routing" : "16",
"_source" : {
"pty_na_id" : 1.1627765E7,
"del_fl" : "N",
"suf" : null,
"pty_id" : 990183115,
"na" : "LAMBERT",
"fst_na" : "MIRANDA",
"sal" : null,
"mid_init" : "L",
"pty_na_typ_cde" : "PP",
"ipi_na_nr" : 4.00942985E8,
"full_na" : "LAMBERT MIRANDA L",
"suf_oth" : null,
"my_join_field" : {
"name" : "name",
"parent" : "pty990183115"
},
"document_type" : "name"
}
},
{
"_index" : "pprepv5",
"_type" : "_doc",
"_id" : "name11627764990183115",
"_score" : 8.721075,
"_routing" : "16",
"_source" : {
"pty_na_id" : 1.1627764E7,
"del_fl" : "N",
"suf" : null,
"pty_id" : 990183115,
"na" : "LAMBERT",
"fst_na" : "MIRANDA",
"sal" : null,
"mid_init" : "LEIGH",
"pty_na_typ_cde" : "PA",
"ipi_na_nr" : 4.00942887E8,
"full_na" : "LAMBERT MIRANDA LEIGH",
"suf_oth" : null,
"my_join_field" : {
"name" : "name",
"parent" : "pty990183115"
},
"document_type" : "name"
}
}
]
}
}
}
}
]
}
}';
begin
apex_json.parse (json);
l_cnt := APEX_JSON.get_count(p_path => 'hits.hits');
DBMS_OUTPUT.put_line('hits.hits Count : ' || l_cnt);
l_cnt_2 := APEX_JSON.get_count(p_path => 'hits.hits.inner_hits.name.hits.hits');
DBMS_OUTPUT.put_line('inner before loop Count : ' || l_cnt_2);
FOR i IN 1 .. l_cnt LOOP
DBMS_OUTPUT.put_line('Counter : ' || i);
DBMS_OUTPUT.put_line('pty_id : ' ||
APEX_JSON.get_varchar2(p_path => 'hits.hits[%d]._source.pty_id', p0 => i));
l_cnt_2 := APEX_JSON.get_count(p_path => 'hits.hits[%d].inner_hits.name.hits.hits');
DBMS_OUTPUT.put_line('inner after loop Count : ' || l_cnt_2);
end loop;
end;
in my sails controller I have implement a function to find users. but when I try to search using ObjectId it returns all the data. I couldn't find any reason. but it works when I create a new separate field as "userID", and find using "userID". I think the problem is find using the "ObjectId" what could be the issue.
module.exports = {
updateOrders : function(req,res){
var ObjectId = require('mongodb').ObjectID;
console.log("req " + req);
var data = req.body;
var obj = [];
console.log("\ndata "+(data));
var jdata = JSON.stringify(data);
console.log("\njdata: "+jdata);
data.forEach(function(orders){
var objid = ObjectId(orders.id);
console.log("Obj orders id :" + objid);
ApplicationOrder.find({id: ObjectId(orders.id)}).exec(function (err, order) {
if (err){
return done(err);
}
console.log('order ' + order);
obj.push(order);
});
})
res.send(obj);
}
};
Json data: printed in the console.
{
"_id" : ObjectId("59a8f820caf1cae37af72c0c"),
"appId" : "59a669431954a69e37c20b69",
"registeredUser" : "59a677811954a69e37c20b73",
"item" : [
{
"id" : "59a669441954a69e37c20b70",
"name" : "Short x",
"qty" : 1,
"sku" : "1111",
"totWeight" : null,
"price" : "250",
"total" : "250",
"imgURL" : [
{
"img" : "short_x.png"
}
],
"totalQty" : 218
}
],
"amount" : "250",
"customerName" : "dilakshan",
"deliverName" : "dilakshan",
"deliveryNo" : "11010",
"deliveryStreet" : "delgoda",
"deliveryCity" : "kandana",
"deliveryCountry" : "Sri Lanka",
"deliveryZip" : "11010",
"telNumber" : 94779967409,
"tax" : "0",
"shippingCost" : "0",
"shippingOpt" : "Flat Rate",
"email" : "s.dilakshan#yahoo.com",
"currency" : "$",
"paymentStatus" : "Pending",
"fulfillmentStatus" : "Pending",
"createdAt" : ISODate("2017-09-01T06:03:12.584Z"),
"updatedAt" : ISODate("2017-09-01T06:03:12.584Z")
}
{
"_id" : ObjectId("59a8f82fcaf1cae37af72c0d"),
"appId" : "59a669431954a69e37c20b69",
"registeredUser" : "59a677811954a69e37c20b73",
"item" : [
{
"id" : "59a669441954a69e37c20b6d",
"name" : "Shirt x",
"qty" : 1,
"sku" : "1111",
"totWeight" : null,
"price" : "250",
"total" : "250",
"imgURL" : [
{
"img" : "shirt_x.png"
}
],
"totalQty" : 244
}
],
"amount" : "250",
"customerName" : "dilakshan",
"deliverName" : "dilakshan",
"deliveryNo" : "11010",
"deliveryStreet" : "delgoda",
"deliveryCity" : "kandana",
"deliveryCountry" : "Sri Lanka",
"deliveryZip" : "11010",
"telNumber" : 94779967409,
"tax" : "0",
"shippingCost" : "0",
"shippingOpt" : "Flat Rate",
"email" : "s.dilakshan#yahoo.com",
"currency" : "$",
"paymentStatus" : "Pending",
"fulfillmentStatus" : "Pending",
"createdAt" : ISODate("2017-09-01T06:03:27.022Z"),
"updatedAt" : ISODate("2017-09-01T06:03:27.022Z")
}
{
"_id" : ObjectId("59a8f83ecaf1cae37af72c0e"),
"appId" : "59a669431954a69e37c20b69",
"registeredUser" : "59a677811954a69e37c20b73",
"item" : [
{
"id" : "59a669441954a69e37c20b71",
"name" : "Beach Suit",
"qty" : 2,
"sku" : "1111",
"totWeight" : null,
"price" : "250",
"total" : "250",
"imgURL" : [
{
"img" : "cloth3.png"
}
],
"totalQty" : 238
}
],
"amount" : "500",
"customerName" : "dilakshan",
"deliverName" : "dilakshan",
"deliveryNo" : "11010",
"deliveryStreet" : "delgoda",
"deliveryCity" : "kandana",
"deliveryCountry" : "Sri Lanka",
"deliveryZip" : "11010",
"telNumber" : 94779967409,
"tax" : "0",
"shippingCost" : "0",
"shippingOpt" : "Flat Rate",
"email" : "s.dilakshan#yahoo.com",
"currency" : "$",
"paymentStatus" : "Pending",
"fulfillmentStatus" : "Pending",
"createdAt" : ISODate("2017-09-01T06:03:42.439Z"),
"updatedAt" : ISODate("2017-09-01T06:03:42.439Z")
}
For standard sails.js/waterline queries, you have to use the id as a String in your query params, do not use ObjectId(ordersId).
When you use a sails native query, use ObjectId(ordersId).
If you are using any mongodb adapter then do not try to convert id by ObjectId(orders.id).Try normal query like
ApplicationOrder.find({'id':orders.id})
For native sails query,you have to convert id to objectId.
I finally was able to fix the issue. I had to edit the my model class in sails and use only orders.id model class,
_id:{type : 'objectid'} and when search
.find({_id: orders.id})
this worked for me perfectly.
I am having a collection orders , inside collection I am having attributes Array , I want to group the records on attributes.value where attributes.name is "sugery"
db.orders.find({},{"attributes":1}).sort({_id:-1}).limit(3);
{
"_id" : ObjectId("599cfdb0f6ec8d00015b0880"),
"attributes" : [
{
"id" : ObjectId("5979a220ad86210001997d5a"),
"name" : "patient",
"status" : NumberInt(1),
"sysDefined" : NumberInt(1),
"value" : "Lincon Burrows"
},
{
"id" : ObjectId("5979a229ad86210001997d5c"),
"name" : "notes",
"status" : NumberInt(1),
"sysDefined" : NumberInt(1),
"value" : ""
},
{
"id" : ObjectId("5979a230ad86210001997d5e"),
"name" : "surgeon",
"status" : NumberInt(1),
"sysDefined" : NumberInt(1),
"value" : "Dr. Williams"
},
{
"id" : ObjectId("5979a238ad86210001997d60"),
"name" : "surgery",
"status" : NumberInt(1),
"sysDefined" : NumberInt(1),
"value" : "Heart Surgery"
}
]
}
{
"_id" : ObjectId("599c357a9ec26c00017892a6"),
"attributes" : [
{
"value" : "new patient",
"sysDefined" : NumberInt(1),
"status" : NumberInt(1),
"name" : "patient",
"id" : ObjectId("5979a220ad86210001997d5a")
},
{
"value" : "",
"sysDefined" : NumberInt(1),
"status" : NumberInt(1),
"name" : "notes",
"id" : ObjectId("5979a229ad86210001997d5c")
},
{
"value" : "http://localhost:4200/#new surgeon",
"sysDefined" : NumberInt(1),
"status" : NumberInt(1),
"name" : "surgeon",
"id" : ObjectId("5979a230ad86210001997d5e")
},
{
"value" : "new surgery",
"sysDefined" : NumberInt(1),
"status" : NumberInt(1),
"name" : "surgery",
"id" : ObjectId("5979a238ad86210001997d60")
}
]
}
{
"_id" : ObjectId("599c2069b2afd900019ea142"),
"attributes" : [
{
"value" : "Mellisa Darwin",
"sysDefined" : NumberInt(1),
"status" : NumberInt(1),
"name" : "patient",
"id" : ObjectId("5979a220ad86210001997d5a")
},
{
"value" : "",
"sysDefined" : NumberInt(1),
"status" : NumberInt(1),
"name" : "notes",
"id" : ObjectId("5979a229ad86210001997d5c")
},
{
"value" : "Dr. Josefina P. Robles",
"sysDefined" : NumberInt(1),
"status" : NumberInt(1),
"name" : "surgeon",
"id" : ObjectId("5979a230ad86210001997d5e")
},
{
"value" : "Spine Surgery",
"sysDefined" : NumberInt(1),
"status" : NumberInt(1),
"name" : "surgery",
"id" : ObjectId("5979a238ad86210001997d60")
}
]
}
I want output like
[
{
"_id":"Heart Surgery",
"count":1
},
{
"_id":"new surgery",
"count":1
},
{
"_id":"Spine Surgery",
"count":1
}
]
Try this :
orders.aggregate({"attributes":1}, {$unwind:"$attributes"},{$match:{"attributes.name":"surgery"}})
please see mongodb documentation for group. db.collection.group()
The following are examples of the db.collection.group() method. The examples assume an orders collection with documents of the following prototype:
{
_id: ObjectId("5085a95c8fada716c89d0021"),
ord_dt: ISODate("2012-07-01T04:00:00Z"),
ship_dt: ISODate("2012-07-02T04:00:00Z"),
item: {
sku: "abc123",
price: 1.99,
uom: "pcs",
qty: 25
}
}
Group by Two Fields
The following example groups by the ord_dt and item.sku fields those documents that have ord_dt greater than 01/01/2012:
db.runCommand({
group:
{
ns: 'orders',
key: { ord_dt: 1, 'item.sku': 1 },
cond: { ord_dt: { $gt: new Date( '01/01/2012' ) } },
$reduce: function ( curr, result ) { },
initial: { }
}
})
i am new in angularjs and mongodb please check below data and code.. i want to show data in console log but its show undefined i don't know why, i was try many times but does not success please guys help me i am very thankful to you please check below code
var current_schedules = this.current.schedule;
if (angular.isArray(current_schedules)) {
for (var j = 0; j < current_schedules.length; j++) {
console.log(current_schedules[j].length);
for (var i = 0; i < current_schedules[j].length; i++) {
console.log(current_schedules[j][i].title);
}
}
}
//MOngodb Data
"schedule" : [
[
{
"_id" : ObjectId("58f76aa9be4d311a78f24f78"),
"sch_end" : ISODate("2017-04-24T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-24T00:00:00.000Z"),
"available" : false,
"title" : "test3"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f77"),
"sch_end" : ISODate("2017-04-25T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-25T00:00:00.000Z"),
"available" : false,
"title" : "test4"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f76"),
"sch_end" : ISODate("2017-04-27T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-27T00:00:00.000Z"),
"available" : false,
"title" : "test6"
}
],
[
{
"_id" : ObjectId("58f76aa9be4d311a78f24f78"),
"sch_end" : ISODate("2017-04-24T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-24T00:00:00.000Z"),
"available" : false,
"title" : "test9"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f77"),
"sch_end" : ISODate("2017-04-25T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-25T00:00:00.000Z"),
"available" : false,
"title" : "test10"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f76"),
"sch_end" : ISODate("2017-04-27T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-27T00:00:00.000Z"),
"available" : false,
"title" : "test11"
}
]
]
when i use console.log(current_schedules); its show data
I have a collection which contains the documents as shown below
{
"_id" : ObjectId("56d92901f9d573cc1c1fb8bb"),
"busEntryExitInformation" : {
"dateTime" : ISODate("2016-03-04T06:19:45.914+0000"),
"busEntryExitEvent" : [
{
"plateNumber" : "ADFN3R2",
"direction" : "EXIT",
"routeNumber" : NumberInt(929),
"driverID" : "DId5",
"driverName" : "john",
"_id" : ObjectId("56d92901f9d573cc1c1fb8c0")
},
{
"plateNumber" : "ADFN3R4",
"direction" : "EXIT",
"routeNumber" : NumberInt(652),
"driverID" : "DId2",
"driverName" : "jack",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bf")
},
{
"plateNumber" : "ADFN3R8",
"direction" : "EXIT",
"routeNumber" : NumberInt(500),
"driverID" : "DId5",
"driverName" : "john",
"_id" : ObjectId("56d92901f9d573cc1c1fb8be")
},
{
"plateNumber" : "ADFN3R7",
"direction" : "ENTRY",
"routeNumber" : NumberInt(500),
"driverID" : "DId3",
"driverName" : "mack",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bd")
},
{
"plateNumber" : "ADFN3R2",
"direction" : "EXIT",
"routeNumber" : NumberInt(652),
"driverID" : "DId2",
"driverName" : "sandesh",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bc")
}
],
"cameraIntrinsics" : {
"cameraFocalLength" : NumberInt(35),
"cameraAngle" : NumberInt(20),
"imageWidth" : "1920",
"imageHeight" : "1080",
"frameRate" : NumberInt(25)
},
"cameraExtrinsics" : {
"cameraId" : NumberInt(1),
"cameraName" : "Under Route-090 NorthboundBridge",
"cameraDirection" : "Towards Northbound Lanes",
"cameraLatitude" : 1.350228,
"cameraLongitude" : 103.984889,
"cameraHeight" : NumberInt(30)
}
},
"__v" : NumberInt(0)
}
}
where busEntryExitEvent is a array i am trying to remove the array elements where direction is exit across all the documents in the collection
result should contain all the documents with direction entry
{
"_id" : ObjectId("56d92901f9d573cc1c1fb8bb"),
"busEntryExitInformation" : {
"dateTime" : ISODate("2016-03-04T06:19:45.914+0000"),
"busEntryExitEvent" : [
{
"plateNumber" : "ADFN3R7",
"direction" : "ENTRY",
"routeNumber" : NumberInt(500),
"driverID" : "DId3",
"driverName" : "mack",
"_id" : ObjectId("56d92901f9d573cc1c1fb8bd")
}
],
"cameraIntrinsics" : {
"cameraFocalLength" : NumberInt(35),
"cameraAngle" : NumberInt(20),
"imageWidth" : "1920",
"imageHeight" : "1080",
"frameRate" : NumberInt(25)
},
"cameraExtrinsics" : {
"cameraId" : NumberInt(1),
"cameraName" : "Under Route-090 NorthboundBridge",
"cameraDirection" : "Towards Northbound Lanes",
"cameraLatitude" : 1.350228,
"cameraLongitude" : 103.984889,
"cameraHeight" : NumberInt(30)
}
},
"__v" : NumberInt(0)
}
}
i am trying to do like this
db.busEntryExitDoc.update(
{ $pull: { busEntryExitEvent: { "direction" : "EXIT"} } },
{ multi: false }
)
this has to be done to all the documents there is no where condition here how to do it please help
The MongoDB update() methods expects three arguments: db.collection.update(query, update, options). If you want to perform the update on all documents, just use an empty object as query:
db.busEntryExitDoc.update({},
{ $pull: { 'busEntryExitInformation.busEntryExitEvent' : {'direction' : "EXIT"} } },
{ multi: true }
);
Also, note the use of the dot notation to access the array field: you have to point the $pull operator to an array otherwise the operation fails. Finally, use { multi: true } as options since you want to update all the documents in your collection.
You are doing it wrong the first argument to .update() is the "query" argument which should be an empty document here.
db.busEntryExitDoc.update(
{},
{ $pull: { "busEntryExitInformation.busEntryExitEvent": { "direction": "EXIT" } } },
{ multi: true }
)
The same thing apply to the .updateMany() method
db.busEntryExitDoc.update(
{},
{ $pull: { "busEntryExitInformation.busEntryExitEvent": { "direction": "EXIT" } } },
)