MongoDB/Mongoose .find() equal to true/false where null==true - mongodb

Trying to simplify this snippet into a one liner.
var conditions = [{key: isTrueOrFalse}];
if (isTrueOrFalse)
conditions.push({key: null});
query.find({$or: conditions});
The key, by default, isn't set in the model.
I wrote this util function to make it a one liner, but I'd prefer to use something built into mongoose if possible.
var findWithDefaultIfNull = function(query, key, value, defaultValue) {
var conditions = [];
var condition = {};
condition[key] = value;
conditions.push(condition);
if (value == defaultValue) {
condition = {};
condition[key] = null;
conditions.push(condition);
}
console.log(conditions);
query.find({$or: conditions});
};
exports.findWithDefaultIfNull = findWithDefaultIfNull;

Related

Dart: CloudFirestore: Firebase -> How to pass firestore query operator as variable Query<T>.where(key, operator, value)

Firestore with Dart requires query operators to be passed as objects.
Docs: https://firebase.google.com/docs/firestore/query-data/queries
Ex Dart:
.where("key", isEqualTo: value)
Ex Go:
.where("key", "==", value).
In the case of go, passing a string "==" as the query operator is pretty straight forward.
For dart, i am trying to figure out how to store isEqualTo: as a variable, to then pass into the function.
Ok here is my code. Any help is really appreciated thank you!
Here is the DataModel
class FirestoreQueryModel extends Equatable {
//Variables
final String property;
final FirestoreOperatorEnum operator;
// This is the value where i want to store the list of operators
final dynamic value;
}
Here is the current repository
class CustomerRepository{
late Query golbalQuery
Stream <List<CustomerModel>> dynmamicCollectonStream(List<FirestoreQueryModel> queryList,) { //If query list is null, do not apply where clause
if (queryList.isEmpty) { return collectionRef().snapshots().map((doc) {var returnedList = doc.docs; var mappedList = returnedList.map((doc) => CustomerModel.fromDocument(doc)).toList();
return mappedList;
});
} else {var count = queryList.length; CollectionReference cRef = collectionRef();
for (var i = 0; i < count; i++) { golbalQuery = cRef.where(queryList[i].property, isEqualTo: queryList[i].value); }
var list = golbalQuery.snapshots().map((doc) { var returnedList = doc.docs; var mappedList = returnedList.map((doc) =\> CustomerModel.fromDocument(doc)).toList(); return mappedList; }); return list; } }}
In the for loop, where we convert the data model to a where clause I currently have hardcoded isEqualTo:
The goal is to convert
for (var i = 0; i < count; i++) { golbalQuery = cRef.where(queryList[i].property, isEqualTo: queryList[i].value); }
to
for (var i = 0; i < count; i++) { golbalQuery = cRef.where(queryList[i].property, qyeryList[i].operator, queryList[i].value); }

Filter data from Stream in Flutter/Dart

I have a Stream which contains List of objects. i.e. List<ProductSubCategoryListModel> and
ProductSubCategoryListModel contains isSelected boolean and CategoryId int.
I want to filter Stream which only has isSelected == true and get list type of List<ProductSubCategoryListModel> in return. After this need to get all the CategoryId from `List.
Here's my code.
Future<void> fetchProducts(
TrendingCategoriesModel categoryModel, int pageIndex) async {
_localCategoryModel = categoryModel;
//Stream I want to manipulate to get List<ProductSubCategoryListModel>
var stream = _categoryListController.stream;
var request = ProductListRequestModel();
var searchParameterModel = SearchParams();
searchParameterModel.SearchText = '';
searchParameterModel.CategoryIDs = //List<int> of CategoryIds filtered from List<ProductSubCategoryListModel>;
searchParameterModel.SubCategoryIDs = null;
searchParameterModel.AttributeIDs = null;
searchParameterModel.SubAttributeIDs = null;
searchParameterModel.StartPrice = 0;
searchParameterModel.EndPrice = null;
searchParameterModel.EndPrice = null;
request.PageIndex = pageIndex;
request.PageSize = 10;
request.SortExpression = '';
request.SortType = '';
request.IsGetFilterData = true;
request.searchParams = searchParameterModel;
if (!_productListController.isClosed) _productListController.sink.add(null);
var response =
await _networkHandler.callAPI(NetworkUrl.PRODUCT_LIST, request);
if (response != null) {
}
}
You can use map method https://api.flutter.dev/flutter/dart-async/Stream/map.html. it returns new stream with mapped data. Map will be applied to each event in a stream.
Additionally you can apply distinct method https://api.flutter.dev/flutter/dart-async/Stream/distinct.html just after map, if you need to ensure that only unique values will be emitted to the subscriber

Get the original query object in Mongoose

I have a loop to perform multiple queries through Mongoose
"use strict";
var Mongoose = require("mongoose");
var User = Mongoose.model("User");
var Cache = {};
for (var index=0; index<usernames.length; index++) {
var query = {
username:usernames[index]
};
User.find(query).
exec(function(error,users){
//THIS IS A CALLBACK FUNCTION,
//HOW TO GET THE 'query' VARIABLE ABOVE?
//I WANT TO PUT THE RESULT INTO CACHE:
var username = users[0].username;
Cache[username] = users[0];
});
}
I need to know which result is of which query, in the callback function above.
It is for db query caching purpose. I can extract 'username' from 'users[0]', but when the array 'users' is empty, there's no such thing.
Put anonymous function inside your loop; and use .findOne() instead of .find() if you are only interested in the first user or if the username values are unique.
for (var index = 0; index < usernames.length; index++) {
(function () {
var query = {
username: usernames[index]
};
User.findOne(query).
exec(function (error, user) {
//use your query here
var username = user.username;
Cache[username] = user;
});
})()
}
However consider async for this kind of operations.

When using NumericField, get absolutely nothing back every time

Been playing with Lucene.NET the last two days.
After reading up on Dates, I was led to believe that Dates are best converted to Milliseconds, and stored in NumericField, with Indexing=true, and Store=No.
But now nothing ever returns - it must be something basic, but I'm just not seeing it.
The saving code is as follows:
...
else if (type == typeof (DateTime?))
{
var typedValue = (DateTime?) value;
field = numericField = new NumericField(documentFieldName, 4, Field.Store.YES, true);
long milliseconds = typedValue.HasValue?(typedValue.Value.Date.Ticks/TimeSpan.TicksPerMillisecond):0;
numericField.SetLongValue(milliseconds);
doc.Add(numericField);
}
...
else
{
field = stringField = new Field(
documentFieldName,
(value != null)?value.ToString():string.Empty,
Store.YES,
Field.Index.ANALYZED) ;
doc.Add(stringField);
}
// Write the Document to the catalog
indexWriter.AddDocument(doc);
When I query for docs against the values saved in Field ... no problem.
When I query for documents by matching against the values in NumericFields, nothing returns.
Where did I go wrong?
Thanks for your help.
Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
var q2 = NumericRangeQuery.NewLongRange("Val", 3, 3, true, true);
var uxy2 = documentSearchManagementService.Search("Students", termQuery, "Id");
Using:
public ScoredDocumentResult[] Search(string indexName, Query query, params string[] hitFieldNamesToReturn)
{
if (_configuration.IndexRootDirectory.IsNullOrEmpty())
{
throw new Exception("Configuration.IndexRootDirectory has not been configued yet.");
}
indexName.ValidateIsNotNullOrEmpty("indexName");
hitFieldNamesToReturn.ValidateIsNotDefault("hitFieldNamesToReturn");
//Specify the index file location where the indexes are to be stored
string indexFileLocation = Path.Combine(_configuration.IndexRootDirectory, indexName);
Lucene.Net.Store.Directory luceneDirectory = Lucene.Net.Store.FSDirectory.Open(indexFileLocation);
IndexSearcher indexSearcher = new IndexSearcher(luceneDirectory);
TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.Create(10, true);
indexSearcher.Search(query, topScoreDocCollector);
List<ScoredDocumentResult> results = new List<ScoredDocumentResult>();
foreach (var scoreDoc in topScoreDocCollector.TopDocs(0, 10).ScoreDocs)
{
ScoredDocumentResult resultItem = new ScoredDocumentResult();
Lucene.Net.Documents.Document doc = indexSearcher.Doc(scoreDoc.Doc);
resultItem.Score = scoreDoc.Score;
List<ScoredDocumentFieldResult> fields = new List<ScoredDocumentFieldResult>();
foreach (string fieldName in hitFieldNamesToReturn)
{
string fieldValue = doc.Get(fieldName);
fields.Add(new ScoredDocumentFieldResult{Key= fieldName,Value=fieldValue});
}
resultItem.FieldValues = fields.ToArray();
results.Add(resultItem);
}
indexSearcher.Close();
return results.ToArray();
}

Extending Javascript objects with CoffeeScript

I want to add the ability to extend javascript objects by adding a method to the prototype.
The method will receive one or more other objects and will add all of the key/values to this.
This is what I came up with:
Object::extend = (objects...) ->
#[key] = value for key, value of object for object in objects
or this:
Object::extend = (objects...) ->
for object in objects
for key, value of object
#[key] = value
Both work as expected, and compile into the same javascript code:
var __slice = [].slice;
Object.prototype.extend = function() {
var key, object, objects, value, _i, _len, _results;
objects = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
_results = [];
for (_i = 0, _len = objects.length; _i < _len; _i++) {
object = objects[_i];
_results.push((function() {
var _results1;
_results1 = [];
for (key in object) {
value = object[key];
_results1.push(this[key] = value);
}
return _results1;
}).call(this));
}
return _results;
};
What I'm not too happy about is the whole results thing that is created per for loop which is completely redundant for my purpose.
Is there a way to get a code more like:
Object.prototype.extend = function() {
var key, object, objects, value, _i, _len;
objects = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
for (_i = 0, _len = objects.length; _i < _len; _i++) {
object = objects[_i];
(function() {
for (key in object) {
value = object[key];
this[key] = value;
}
}).call(this);
}
};
Thanks.
Edit
I'm aware that I can simply embed javascript code, but looking for a coffeescript solution.
You can try adding an explicit return:
Object::extend = (objects...) ->
for object in objects
for key, value of object
#[key] = value
return
That produces this:
var __slice = [].slice;
Object.prototype.extend = function() {
var key, object, objects, value, _i, _len;
objects = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
for (_i = 0, _len = objects.length; _i < _len; _i++) {
object = objects[_i];
for (key in object) {
value = object[key];
this[key] = value;
}
}
};
Every CoffeeScript function returns the value of the last expression in the function, CoffeeScript loops are also expressions. That means that CoffeeScript has to build all that _results stuff to produce a return value for your function since you had an implicit return applying to the outer loop. If you remove that implicit return by adding an explicit "return nothing" then the CS→JS compiler seems to be smart enough to not do all that extra _results work.