mongodb query script by js. with condition as an array to iterate - javascript

db = db.getSiblingDB("aqua") //same as use otherdb
var legalStatus=["initiate","process","complete","replicating","failed","offline_complete","offline_fail","invalid"];
var replicas=db.aquaObject.find({"replicas":{$ne:null},"replicas.status":{$nin:legalStatus}});
replicas.forEach(
function(t){
db.aquaObject.update(t,{$set:{"replicas.0.status":"initiate"}});
});
this is the script that I wrote. But the condition in the find method: "replicas.status",here the replicas is an array. I want to iterator all the replicas and filter each one's status. So I believe what I write is not correct. How can I refine it? Thanks.

Related

getting distinct values from result set in taffyDB

I have a TAFFYDB database that consists of 4 fields:
clientID
cTech
arDate
active
What I want is a unique list of "cTech" for a certain clientID inside of a range of dates.
I can match clientID and dates like this:
var ret=clientTechsDB([{
"clientID":cFrom.toLowerCase(),
"arDate":{gte:sDfrom},
"arDate":{lte:sDto},
}]).get();
That returns the array "ret", but ret has many duplicated cTech values.
I tried
var ret=clientTechsDB([{
"clientID":cFrom.toLowerCase(),
"arDate":{gte:sDfrom},
"arDate":{lte:sDto},
}]).get().distinct("cTech");
but that generates and error "get(...).distinct is not a function"
I can iterate through and filter out duplicates but I was hoping to do it in a taffyDB query.
How?
You don't need "get" when using distinct. The correct syntax is:
var ret=clientTechsDB([{
"clientID":cFrom.toLowerCase(),
"arDate":{gte:sDfrom},
"arDate":{lte:sDto},
}]).distinct("cTech");

Javascript object with arrays to search param style query string

Looking for clean way to convert a javascript object containing arrays as values to a search param compatible query string. Serializing an element from each array before moving to the next index.
Using libraries such as querystring or qs, converts the object just fine, but handles each array independently. Passing the resulting string to the server (which I cannot change) causes an error in handling of the items as each previous value is overwritten by the next. Using any kind of array notation in the query string is not supported. The only option I have not tried is a custom sort function, but seems like it would be worse than writing a custom function to parse the object. Any revision to the object that would generate the expected result is welcome as well.
var qs = require("qs")
var jsobj = {
origString:['abc','123'],
newString:['abcd','1234'],
action:'compare'
}
qs.stringify(jsobj,{encode:false})
qs.stringify(jsobj,{encode:false,indices:false})
qs.stringify(jsobj,{encode:false,indices:false,arrayFormat:'repeat'})
Result returned is
"origString=abc&origString=123&newString=abcd&newString=1234&action=compare"
Result desired would be
"origString=abc&newString=abcd&origString=123&newString=1234&action=compare"
I tried reorder your json:
> var jsobj = [{origString: 'abc', newString: 'abcd' }, {origString: '123',
newString: '1234' }, {action:'compare'}]
> qs.stringify(jsobj,{encode:false})
'0[origString]=abc&0[newString]=abcd&1[origString]=123&1[newString]=1234&2[action]=compare'
But I don't know if this is a good alternative for your problem.
Chalk this up to misunderstanding of the application. After spending some more time with the API I realized my mistake, and as posted above by others, order does no matter. Not sure why my first several attempts failed but the question is 'answered'

How to skip iteration in collection runner with data file in postman

Is it possible to skip (or repeat once again) the iteration through the collection using JSON data file in the Collection Runner like:
if(pm.environment.get("skip").to.eql("yes"){
\\pm.iterationData.GOTOITERATION(2) <--PSEUDOCODE
}
I was thinking that if I would be able to access the whole datafile (array of objects), it will be possible to write such thing:
var currentIterationData;
function ChangeCurrentIteration(iterationNumber)
{ currentIterationData =
data[iterationNumber] // here I want to access element of data's array
}
But don't I go in wrong direction? does my question have sence? thank you.
You could achieve this with the below code:
var testID =pm.iterationData.get("testID");
if(testID.includes("<substring>")) {
postman.setNextRequest(null);
};
Where the "testID" is one of the key value pair in data file and pm.iterationData.get("key") method retrieves the value. Based on the condition, the next request will be skipped and next iteration will continue. You could also repeat a particular request by adding:
postman.setNextRequest(<requestToRepeat>);

How to create a copy of mongoose's Aggregate object?

I need a way to duplicate and dynamically modify my requests to the database. With regular mongoose Query objects I can create a copy of the query with x = query.toConstructor() and later fire multiple requests with additional parameters, e.g.:
var sample = x().limit(5);
var totalCount = x().count();
However mongoose's Aggregate objects lack the toConstructor function. Is there any way to achieve same results with an Aggregate object?
The following solution worked for me well, though not so nice.
const withoutId = Model.aggregate().project({ _id: false });
const totalCount = Model.aggregate(withoutId.pipeline()).count('count');
Hope this will help at least others.

Parse.com : Query Max Value of Column in JS API

If in sql, we can get it with:
select Max(Column_name) from Table_name
but still can't find simple way to get Max value of Column in Parse.com JS API.
Please explain me how to get Max Value of Column in JS API?
The best way to do this is to use Parse.Query (api) and order by descending, then obtain the first item in the result.
Edit:
Maybe, it's not a good idea to use order by in the situation that there are thousands(or even more) items as it's time consuming(complexity at least O(nlogn)). An alternative is to write a function of your own to choose the maximum value yourself with complexity of O(n).
Write a query for descending order and fetch the first object out of it. You will have the max value of the column there.
var query = new Parse.Query("something");
query.descending("column_name");
query.first({
success: function(result){
var max = result.get("column_name");
},
error: function(){
},
})
I am not sure if you can do it directly with a max function. You can create your own max function by doing a query to get all entries from that column and sorting them in descending order. There are APIs for this. Then choose the first value from the sorted list.
You can basically follow the same pattern from all other queries:
// Query for ID
ParseQuery<ParseObject> query = ParseQuery.getQuery("Table_Name");
// Condition
query.orderByDescending("ID");
// First object will be retrieved, this will be the max_value
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
Log.d("score", "The getFirst request failed.");
} else {
Log.d("score", "Retrieved the object.");
}
Now you can get the value using something like:
String mensagem = object.getString("ID");
Hopefully this will help you!

Categories

Resources