How can I get documents with some conditions? - mongodb

I have some objects:
var array = [];
var obj1 = new Object();
obj1.category = 0;
obj1.sequence = 0;
obj1.otherAttr = 1;
array.push(obj1)
var obj2 = new Object();
obj2.category = 1;
obj2.sequence = 2;
obj1.otherAttr = 2;
array.push(obj2)
var obj3 = new Object();
obj3.category = 4;
obj4.sequence = 3;
obj1.otherAttr = 3;
array.push(obj3)
...
and I have collection in MongoDB:
{
category:0,
sequence:0,
anotherAttr:1
},
{
category:0,
sequence:1,
anotherAttr:2
},
{
category:0,
sequence:2,
anotherAttr:3
},
{
category:1,
sequence:0,
anotherAttr:4
},
{
category:1,
sequence:1,
anotherAttr:5
},
{
category:1,
sequence:2,
anotherAttr:6
},
...
I wanna insert these objects. But Before insert these, I want check overlapping category and sequence between these and document in MongoDB unrelated to otherAttr or anotherAttr. For example, I have objects like top, the result I need is :
{
category:0,
sequence:0,
anotherAttr:1
},
{
category:1,
sequence:1,
anotherAttr:5
}
I tried to $match. But can't check some conditions in many attributes.

Related

How to generate leaflet control from database

I wish to generate a custom dropdown filter, based on categories from a database.
How is this achieved?
In my example, this is (poorly) implemented with some hard coding and duplication.
var serviceOverlays = [
{name:"Cardiology", value:"cardiology"},
{name:"Opthamology", value:"opthamology"}
];
var oSelect = L.control({position : 'topright'});
oSelect.onAdd = function (map) {
var overlayParent = document.getElementById('new-parent'); // overlays div
var node = L.DomUtil.create('select', 'leaflet-control');
node.innerHTML = '<option value="cardiologist">Cardioligist</option><option value="opthamology">Opthamology</option>';
overlayParent.appendChild(node);
L.DomEvent.disableClickPropagation(node);
L.DomEvent.on(node,'change',function(e){
var select = e.target;
for(var name in serviceOverlays){
serviceOverlays[name].removeFrom(map);
}
serviceOverlays[select.value].addTo(map);
});
Fiddle
I created a Control for you:
L.Control.Select = L.Control.extend({
options: {
position : 'topright'
},
initialize(layers,options) {
L.setOptions(this,options);
this.layers = layers;
},
onAdd(map) {
this.overlayParent = L.DomUtil.create('div', 'leaflet-control select-control');
this.node = L.DomUtil.create('select', 'leaflet-control',this.overlayParent);
L.DomEvent.disableClickPropagation(this.node);
this.updateSelectOptions();
L.DomEvent.on(this.node,'change',(e)=>{
var select = e.target;
for(var value in this.layers){
this.layers[value].layer.removeFrom(map);
}
this.layers[select.value].layer.addTo(map);
});
return this.overlayParent;
},
updateSelectOptions(){
var options = "";
if(this.layers){
for(var value in this.layers){
var layer = this.layers[value];
options += '<option value="'+value+'">'+layer.name+'</option>';
}
}
this.node.innerHTML = options;
},
changeLayerData(layers){
this.layers = layers;
this.updateSelectOptions();
}
});
var oSelect = new L.Control.Select(serviceOverlays,{position : 'topright'}).addTo(map);
The data structure have to be:
var serviceOverlays = {
"cardiology": {name:"Cardiology", layer: cities},
"opthamology": {name:"Opthamology", layer: badCities}
};
Demo: https://jsfiddle.net/falkedesign/1rLntbo5/
You can also change the data dynamicl< with oSelect.changeLayerData(serviceOverlays2)

How to make batch queue with arrayList when loop?

let's say I have this array:
array = [1,2,3,4,5,6,7,8,9,10];
Then I want to divide this array for each 2 batch with 5 array value:
a[0] = [1,2,3,4,5];
a[1] = [6,7,8,9,10];
This is my code until now keep trying.It should insert to db when batch has been slice for each job for 100 array value.
BatchList = new Meteor.Collection('batch_list');
var arrayAssetList = Asset.find({}, { _id: 1});
var data = [];
arrayAssetList.forEach(function(key){
data.push(key._id)
});
var arraySize = arrayAssetList.count() / 100;
var slice, sliceTo;
for(var i=0; i<arraySize; i++){
if(i == 0){
slice = 0
} {
slice = 100*i
}
if(i>1){
sliceTo = 100*i;
}
BatchList.insert({ data: data.slice(slice,sliceTo)});
}
it should be like this:
[{
_id: '()#()##)#*()#*#',
size: 100,
data: [1,2,3,4,100]
},{
_id: 'sdfsdfnnn3',
size: 100,
data: [100,101,102,103,200]
}]

Swift How to add key value in NSMutable array

I have one nsmutable Array , How to add one keyvalue in nsmutable array at index number
This is array
(
{
id = 1;
name = "Tommy";
},
{
id = 2;
name = "Dad";
},
{
id = 3;
name = "Mom";
},
{
id = 4;
name = "Vic";
},
{
id = 5;
name = "Tom";
},
{
id = 6;
name = "Pam";
}
)
Now Index number 3 how to add "Class = 4th" and change name
{
id = 4;
name = "Nishant ";
class = "4th"
}
Please give ME solution
i am new in Swift.

How do query among many collections then write result to corresponding collection

If I want to query users' age > 18,
and export result to corresponding collection,
How could I do it by rewriteing the following script?
The following is psuedo code
source_collections = ["user_1", "user_2", ..., "user_999"]
output_collections = ["result_1", "result_2", ..., "result_999"]
pipeline = [
{
"$match":{"age": > 18}
}
{ "$out" : output_collections }
]
cur = db[source_collections].runCommand('aggregate',
{pipeline: pipeline,allowDiskUse: true})
The script you're looking for is something like:
var prefix_source = 'user_';
var prefix_output = 'result_';
var source_collections = [];
var output_collections = [];
var numCollections = 999;
for (var i = 1; i <= numCollections; i++) {
source_collections.push(prefix_source + i);
output_collections.push(prefix_output + i);
}
var pipeline = [{'$match': {age: {'$gt': 18}}}, {'$out': ''}]; for (var currentCollection = 0; currentCollection < source_collections.length; currentCollection++) {
pipeline[pipeline.length - 1]['$out'] = output_collections[currentCollection];
var cur = db[source_collections[currentCollection]].runCommand('aggregate', {pipeline: pipeline,allowDiskUse: true});
}
And while you're at it, the var cur = ... line could be simplified to
db[source_collections[currentCollection]].aggregate(pipeline, {allowDiskUse: true});
Note: I've added a piece that generates your arrays for you, as I'm sure you're not looking to write them by hand :D

How to add or inc to an array in a update query?

If I have a doc which has an array which contains a items which represents counts for a day, perhaps like :-
{
data : [ {'20141102' : 2 },{'20141103' : 4 } ]
}
when I do an update, and I have a string '20141103' and then later a '20141104' I want to either inc the array entry or add a new array entry. Is this possible with an update?
Yes, it's feasible. I tried like this:
(run on mongo shell; both client and server are V2.6.4)
function tryAndFine(coll, key, value) {
var entry = {};
entry[key] = value;
var parent = 'data';
var prefix = parent + '.';
function incrementOnly() {
var criteria = {};
criteria[prefix + key] = {$exists : true};
var update = {};
update[prefix + "$." + key] = value;
var result = coll.update(criteria, {$inc : update});
// if increment fails, try to add a new one
if (result.nModified == 0) {
addNewElement();
}
}
function addNewElement() {
var criteria = {};
criteria[prefix + key] = {$exists : false};
var update = {};
update[parent] = entry;
var result = coll.update(criteria, {$push : update}, {upsert : true});
// if exists, try to increment the count
if (result.upserted == 0 && result.nModified == 0) {
incrementOnly();
}
}
// run entry
incrementOnly();
}
// test
var c = db.c;
c.drop();
tryAndFine(c, '20141103', 1);
tryAndFine(c, '20141103', 1);
tryAndFine(c, '20141104', 1);
tryAndFine(c, '20141105', 1);
tryAndFine(c, '20141104', 1);
// output
{
"_id" : ObjectId("54577e1a3502852bd4ad2395"),
"data" : [ {
"20141103" : 2
}, {
"20141104" : 2
}, {
"20141105" : 1
} ]
}