So I have a mongoose schema that looks like this.
var info = new mongoose.Schema(
{ '123': { available: { type: 'String' }, onOrder: { type: 'String' } },
'456': { available: { type: 'String' }, onOrder: { type: 'String' } },
'789': { available: { type: 'String' }, onOrder: { type: 'String' } }
});
I'm looking to find and number in the available field that is > 0. Only some of these will be filled at any given time. The "empty" String will always be filled with a number value of "0.00000000" if not some integer in it.
I'm looking for a query that will go through and find and string that isn't 0.00000000 or can convert the strings to integers then can find anything > 0.
I feel like my first thought of finding them based on strings is kinda hacky in a way and I'm looking for a more permanent way of retrieving numbers from a Database like this.
I've Attempted to accomplish this with $in already but I'm not able to get it to work.
you can do this in two ways, 1. $regexp 2. $where
$regexp
check all the string contents are numbers [0-9], and start with non zero or zero or dot followed by 0-9
regexp would be /^[1-9][0-9]|[.0][1-9]/
$where
use parseFloat check it's greater than zero
{ $where: function() { return parseFloat(this[123].available) > 0 } }
collection
> db.info.find()
{ "_id" : ObjectId("5a5edabdfee4c182f509f3ea"), "123" : { "available" : "0.00000000", "onOrder" : { "type" : "xyz" } } }
{ "_id" : ObjectId("5a5edabdfee4c182f509f3eb"), "123" : { "available" : "0.00000010", "onOrder" : { "type" : "xyz" } } }
{ "_id" : ObjectId("5a5edabdfee4c182f509f3ec"), "123" : { "available" : "100000000", "onOrder" : { "type" : "xyz" } } }
{ "_id" : ObjectId("5a5edabdfee4c182f509f3ed"), "123" : { "available" : "abc", "onOrder" : { "type" : "xyz" } } }
>
find with $regexp
> db.info.find( { "123.available" : /^[1-9][0-9]|[.0][1-9]/ } );
{ "_id" : ObjectId("5a5edabdfee4c182f509f3eb"), "123" : { "available" : "0.00000010", "onOrder" : { "type" : "xyz" } } }
{ "_id" : ObjectId("5a5edabdfee4c182f509f3ec"), "123" : { "available" : "100000000", "onOrder" : { "type" : "xyz" } } }
>
find with $where
> db.info.find( { $where: function() { return parseFloat(this[123].available) > 0 } } );
{ "_id" : ObjectId("5a5edabdfee4c182f509f3eb"), "123" : { "available" : "0.00000010", "onOrder" : { "type" : "xyz" } } }
{ "_id" : ObjectId("5a5edabdfee4c182f509f3ec"), "123" : { "available" : "100000000", "onOrder" : { "type" : "xyz" } } }
>
>
Related
Here is MongoDB scheme.
{
"_id" : ObjectId("222222"),
"active" : false,
"amount" : "15%",
"description" : "15% discount",
"name" : "20200628-test",
"policies" : {
"apply" : [
{
"name" : "expiryDate",
"params" : {
"date" : ISODate("2020-07-06T14:59:59.999Z")
}
},
{
"name" : "isApplyCategoryExist"
}
],
"discount" : [],
"conflict" : [
{
"name" : "exclusive"
}
],
"grant" : []
},
"target" : {
"sku" : "",
"products_ids" : [],
"category_ids" : [
ObjectId("11111111")
]
},
"title" : "15% coupon"
}
I want to access date.
For example, "policies.apply.params.date"...
I don't know how to access 'date' to Javascript.
Please let me know...
apply is an array, so you have to give it index which you want to get.
var num = 0; // pick up an array number you want
var date = policies.apply[num].params.date;
Your policies.apply is an array so if you want to access "2020-07-06T14:59:59.999Z", you should do this:
policies.apply[0].params.date
But the "policies.apply[1]" doesn't have params (params.date also) so you can write a function to get date like this:
function get_apply_date(index) {
if(policies.apply[index].params && policies.apply[index].params.date)
return policies.apply[index].params.date;
return undefined; // or null
}
I have some json schema files describing the structure of my objects. Here is an example:
{
"$schema" : "https://json-schema.org/draft/2019-09/schema",
"$ref" : "#/$defs/AdaugaPersoana",
"$defs" : {
"AdaugaPersoana" : {
"type" : "object",
"properties" : {
"id" : {
"type" : "string"
},
"numeComplet" : {
"$ref" : "#/$defs/NumeComplet"
},
"stare" : {
"$ref" : "#/$defs/Stare"
}
}
},
"NumeComplet" : {
"type" : "object",
"properties" : {
"nume" : {
"type" : "string"
},
"prenume" : {
"type" : "string"
},
"factory()" : {
"type" : "string"
}
},
"required" : [ "nume" ]
},
"Stare" : {
"type" : "string",
"enum" : [ "ACTIVAT", "DEACTIVAT" ]
}
}
}
Is there any way that WebStorm can use this schema to autocomplete when using a JavaScript variable?
Example JS code:
/** #type AdaugaPersoana */
const someVar = {};
someVar.numeComplet.nume //this should be autocompleted
I'm using nodejs with official mongodb's package. I got many documents in mongodb containing "type" and "timestamp" field. I want to sort it by prioritizing "type" (only specific content) and then "timestamp".
As example I have following documents:
{ type: "book", timestamp: 1580825471 }
{ type: "house", timestamp: 1580825502 }
{ type: "water", timestamp: 1580825515 }
{ type: "book", timestamp: 1580825478 }
{ type: "smartphone", timestamp: 1580825522 }
{ type: "book", timestamp: 1580825424 }
My goal is to have sorted by that way to priority the type "book" first (and then sort it by timestamp)
{ type: "book", timestamp: 1580825478 }
{ type: "book", timestamp: 1580825471 }
{ type: "book", timestamp: 1580825424 }
{ type: "smartphone", timestamp: 1580825522 }
{ type: "water", timestamp: 1580825515 }
{ type: "house", timestamp: 1580825502 }
I was trying to use the db.collection.aggregate with following $sort value:
$sort: {
type: "book",
timestamp: -1
}
But that didn't worked out because the $sort field's value can only have the value of "1", "-1" or "{ $meta: "textScore" }".
Does anybody have an idea how to solve that issue?
Thanks in advance
EDIT:
This solution by using
$sort: {
type: 1,
timestamp: -1
}
is not a solution since then all types are also sorted which I don't want it. I just want to have "book" as first result then after that, types can be randomized (but timestamp is still being sorted.). Reason for that is that I want to list history entries (that's why I'm using timestamp to sort it), but I want to show type "book" at first. Even if the document are older than other documents.
So yeah, for other types expect "book", I want it to be sorted by timestamp.
You can add an extra field in a project stage that creates a sort priority, then use that to sort on.
For example:
db.data.aggregate([
{ $addFields : { sortPriority: { $eq: [ "$type", "book" ] } } },
{ $sort: { sortPriority: -1, timestamp: -1} }
])
This will output the following:
{ "_id" : ObjectId("5e39892e0f18de54afe4d874"), "type" : "book", "timestamp" : 1580825478, "sortPriority" : true }
{ "_id" : ObjectId("5e39892e0f18de54afe4d871"), "type" : "book", "timestamp" : 1580825471, "sortPriority" : true }
{ "_id" : ObjectId("5e39892e0f18de54afe4d876"), "type" : "book", "timestamp" : 1580825424, "sortPriority" : true }
{ "_id" : ObjectId("5e39892e0f18de54afe4d875"), "type" : "smartphone", "timestamp" : 1580825522, "sortPriority" : false }
{ "_id" : ObjectId("5e39892e0f18de54afe4d873"), "type" : "water", "timestamp" : 1580825515, "sortPriority" : false }
{ "_id" : ObjectId("5e39892e0f18de54afe4d872"), "type" : "house", "timestamp" : 1580825502, "sortPriority" : false }
If you want to ommit the extra field add $unset stage:
db.data.aggregate([
{ $addFields : { sortPriority: { $eq: [ "$type", "book" ] } } },
{ $sort: { sortPriority: -1, timestamp: -1} },
{ $unset: "sortPriority" }
])
This will then output:
{ "_id" : ObjectId("5e39892e0f18de54afe4d874"), "type" : "book", "timestamp" : 1580825478 }
{ "_id" : ObjectId("5e39892e0f18de54afe4d871"), "type" : "book", "timestamp" : 1580825471 }
{ "_id" : ObjectId("5e39892e0f18de54afe4d876"), "type" : "book", "timestamp" : 1580825424 }
{ "_id" : ObjectId("5e39892e0f18de54afe4d875"), "type" : "smartphone", "timestamp" : 1580825522 }
{ "_id" : ObjectId("5e39892e0f18de54afe4d873"), "type" : "water", "timestamp" : 1580825515 }
{ "_id" : ObjectId("5e39892e0f18de54afe4d872"), "type" : "house", "timestamp" : 1580825502 }
You can create a sorting key by your own:
db.col.aggregate([
{
$addFields: {
sortBy: {
$cond: {
if: { $eq: ["$type", "book"] }, then: 0, else: 1
}
}
}
},
{ $sort: { sortBy: 1, timestamp: 1 } },
{ $unset: "sortBy" }
])
Output:
{ "_id" : ObjectId("5e398952227b6d209de231bb"), "type" : "book", "timestamp" : 1580825424, "sortPriority" : true }
{ "_id" : ObjectId("5e398952227b6d209de231b6"), "type" : "book", "timestamp" : 1580825471, "sortPriority" : true }
{ "_id" : ObjectId("5e398952227b6d209de231b9"), "type" : "book", "timestamp" : 1580825478, "sortPriority" : true }
{ "_id" : ObjectId("5e398952227b6d209de231b7"), "type" : "house", "timestamp" : 1580825502, "sortPriority" : false }
{ "_id" : ObjectId("5e398952227b6d209de231b8"), "type" : "water", "timestamp" : 1580825515, "sortPriority" : false }
{ "_id" : ObjectId("5e398952227b6d209de231ba"), "type" : "smartphone", "timestamp" : 1580825522, "sortPriority" : false }
I've got a lot of doc filters on my UI (date ranges, checkboxes, input fields), so the query is generated dynamically - that's why I decided to create a boolean query, and push everything to must array. This is the example of my request:
const {
body: {
hits
}
} = await esclient.search({
from: filterQuery.page || 0,
size: filterQuery.limit || 1000,
index,
body: query
});
Checkboxes (I used additional bool.should inside must array) and date range work perfectly, but term/match filtering is not working at all:
{
"query": {
"bool": {
"must": [
{"match": { "issueNumber": "TEST-10" }}
]
}
}
}
The query above gives me all the documents from the index that contains "TEST" (with their scores), if I change match to term - it returns an empty array.
As my field is of a type 'text', I've also tried filter query - ES still gives all the documents with 'TEST' word:
{
"query": {
"bool": {
"must": [
{
"bool": {
"filter": {
"match": {"issueNumber": "TEST-10"}
}
}
}
]
}
}
}
This is how my hit looks like:
{
"_index" : "test_elastic",
"_type" : "_doc",
"_id" : "bj213hj2gghg213",
"_score" : 0.0,
"_source" : {
"date" : "2019-11-26T13:27:01.586Z",
"country" : "US",
"issueNumber" : "TEST-10",
}
Can someone give me input on how to filter the docs properly in complex query?
This is the structure of my index:
{
"test_elasticsearch" : {
"aliases" : { },
"mappings" : {
"properties" : {
"country" : {
"type" : "text"
},
"date" : {
"type" : "date"
},
"issueNumber" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1574759226800",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "PTDsdadasd-ERERER",
"version" : {
"created" : "7040299"
},
"provided_name" : "logs"
}
}
}
}
Ok, the problem is that your issueNumber field has not the right type, it should be keyword instead of text if your goal is to make exact searches on it. Same for country. Modify your mapping like this:
"properties" : {
"country" : {
"type" : "keyword"
},
"date" : {
"type" : "date"
},
"issueNumber" : {
"type" : "keyword"
}
}
Then reindex your data and your queries will work.
I have a collection of the structure as follows:
collection name : "positions"
Structure
{
"_id" : "vtQ3tFXg8THF3TNBc",
"candidatesActions" : {
"sourced" : [ ],
},
"appFormObject" : {
"name" : "✶ mandatory",
"questions" : [
{
"qusId" : "qs-585494",
"type" : "simple",
"qus" : "Which was your previous company"
},
{
"qusId" : "qs-867766",
"type" : "yesNo",
"qus" : "Are you willing to relocate?",
"disqualify" : "true"
}
]
}
}
I want to update "qus" field of the above collection whose _id is "vtQ3tFXg8THF3TNBc" and "qusId" is "qs-585494".
Try following....
db.positions.update(
{_id: "vtQ3tFXg8THF3TNBc", "appFormObject.questions.qusId":"qs-585494"},
{$set:{"appFormObject.questions.$.qus": "this is updated value"}}
)
Use following query
db.positions.findAndModify({
query: { _id: "vtQ3tFXg8THF3TNBc", "appFormObject.questions.qusId":"qs-585494"} ,
update: { $set: { 'appFormObject.questions.$.qus': 'Brilliant Green' } },
});
Thanks