简体   繁体   English

Mongoose / MongoDB查询.find()是否有int> 0?

[英]Mongoose / MongoDB query .find() any int > 0?

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. 我想在> 0的可用字段中查找并编号。在任何给定时间仅填充其中一些。 The "empty" String will always be filled with a number value of "0.00000000" if not some integer in it. 如果其中没有整数,则“空”字符串将始终用数字值“ 0.00000000”填充。

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. 我正在寻找一个查询,它将查询并找到不是0.00000000的字符串,或者可以将字符串转换为整数,然后可以找到> 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. 我已经尝试使用$ in完成此操作,但无法使其正常工作。

you can do this in two ways, 1. $regexp 2. $where 您可以通过两种方式执行此操作:1. $regexp 2. $where

  1. $regexp

check all the string contents are numbers [0-9], and start with non zero or zero or dot followed by 0-9 检查所有字符串内容是否为数字[0-9],并以非零或零或点开头,后跟0-9

regexp would be /^[1-9][0-9]|[.0][1-9]/ regexp将是/^[1-9][0-9]|[.0][1-9]/

  1. $where

use parseFloat check it's greater than zero 使用parseFloat检查它是否大于零

{ $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 $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 $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" } } }
> 
> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM