mongodb datetime查询开始日期和结束日期

时间:2019-04-22 17:15:28

标签: mongodb mongoose

它是我的mongodb表结构。

<?php
    include('databasecon.php');

    // GET API JSON DATA
    $coinData = json_decode(file_get_contents('https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,XRB,IOTA,XRP,XLM,TRX,LINK,USDT&tsyms=USD'), true);

    //SELECT ALL MAIL
    $result = mysqli_query($con, "SELECT DISTINCT email FROM user_data");
    $email_array = array();
    while($row = mysqli_fetch_array($result)){
        $email_array[] = $row['email'];
    }

    // PORTFOLIO ARRAYS
    for ($i = 0; $i < sizeof($email_array); $i++) {
        $sql = mysqli_query($con, "SELECT DISTINCT * FROM crypto_data WHERE email = '$email_array[$i]'");
        while($row = mysqli_fetch_array($sql)){
            $myCoins[$row['currency']] = array('balance' => $row['amount'], 'boughtprice' => $row['price_when_bought']);
        }

        // 0 VALUES FOR CALCULATION
        $portfolioValue = 0;
        $totalNET = 0;
        $Value24H = 0;

        // information in json path ['RAW'] so safeguard here to be sure it exists
        if (isset($coinData['RAW'])) {
            // then loop on all entries $cryptoSymbol will contain for example BTC and cryptoInfo the array USD => [...]
            foreach($coinData['RAW'] as $cryptoSymbol => $cryptoInfo) {
                // safeguard, check path [USD][FROMSYMBOL] exists
                if (!isset($cryptoInfo['USD']) || !isset($cryptoInfo['USD']['FROMSYMBOL'])) {
                    // log or do whatever to handle error here
                    echo "no path [USD][FROMSYMBOL] found for crypto: " . $cryptoSymbol . PHP_EOL;
                    continue;
                }

                // Symbol in on your json path/array [USD][FROMSYMBOL]
                $thisCoinSymbol = $cryptoInfo['USD']['FROMSYMBOL'];
                $coinHeld = array_key_exists($thisCoinSymbol, $myCoins);
                // Only retour held
                if ( !$coinHeld ) { continue; }

                // get price:
                $thisCoinPrice = $cryptoInfo['USD']['PRICE'];

                // get symbol holding:
                if ($coinHeld) {
                    $myBalance_units = $myCoins[$thisCoinSymbol]['balance'];
                }

                // calculate total holdings:
                if ($coinHeld) {
                    $myBalance_USD = $myBalance_units * $thisCoinPrice;
                    $portfolioValue += $myBalance_USD;
                }
            }
        }
        echo '<br>';
        echo $email_array[$i];
        echo $portfolioValue . PHP_EOL;
        echo '<br>';
        echo '<br>';
        $portfolioValue = 0;
    }
?>

它在mongodb中的用户表。我需要找到没有顺序的用户 从'2018-04-22 23:14:00'到'2018-04-22 23:50:00'

2 个答案:

答案 0 :(得分:0)

根据您的要求,您可能需要elemMatch运算符,有关详细信息,请查看official documentation

使用此方法,您可以在以下查询集合中执行以下操作:

 db.collcationName.find(
    {"Order" : 
        {$elemMatch: 
            {"start_date": {$not: { $gte: new ISODate('2018-04-22 23:14:00')} }, 
             "end_date": {$not: {$lt: new ISODate('2018-04-22 23:50:00')} } 
            } 
        } 
    })

答案 1 :(得分:0)

您需要过滤并计算“无用”订单(“ 2018-04-22 23:14:00”到“ 2018-04-22 23:50:00”之间的订单),然后过滤没有“无用”订单的用户“订单。

您可以像这样使用聚合

db.User.aggregate(
{
    $project: {
        'name': 1,
        Orders: {
            $filter: {
                input: '$Orders',
                as: 'order',
                cond: {
                    $or: [{
                            $and: [{
                                    $gte: ['$$order.end_date', ISODate('2019-04-22 23:14:00')]
                                },
                                {
                                    $lt: ['$$order.start_date', ISODate('2019-04-22 23:14:00')]
                                }
                            ]
                        },
                        {
                            $and: [{
                                    $gte: ['$$order.end_date', ISODate('2019-04-22 23:50:00')]
                                },
                                {
                                    $lt: ['$$order.start_date', ISODate('2019-04-22 23:50:00')]
                                }
                            ]
                        },
                        {
                            $and: [{
                                    $gte: ['$$order.start_date', ISODate('2019-04-22 23:14:00')]
                                },
                                {
                                    $lt: ['$$order.start_date', ISODate('2019-04-22 23:50:00')]
                                }
                            ]
                        }
                    ]
                }
            }
        }
    }
}, {
    $project: {
        'name': 1,
        'useless_orders': {
            $size: '$Orders'
        }
    }
}, {
    $match: {
        'useless_orders': 0
    }
}
)

保证适用于数据集:

> db.Users.find().pretty()
{
    "_id" : ObjectId("5cbeb3ee382b79e4100195a6"),
    "name" : "kate",
    "Orders" : [
        {
            "start_date" : ISODate("2019-04-22T20:08:39.927Z"),
            "end_date" : ISODate("2019-04-22T23:08:39.927Z")
        },
        {
            "start_date" : ISODate("2019-04-22T20:08:39.927Z"),
            "end_date" : ISODate("2019-04-22T23:08:39.927Z")
        },
        {
            "start_date" : ISODate("2019-04-22T20:08:39.927Z"),
            "end_date" : ISODate("2019-04-22T23:08:39.927Z")
        }
    ]
}
{
    "_id" : ObjectId("5cbeb597cf56f3e410320a96"),
    "name" : "karim",
    "Orders" : [
        {
            "start_date" : ISODate("2019-04-22T20:08:39.927Z"),
            "end_date" : ISODate("2019-04-22T23:08:39.927Z")
        },
        {
            "start_date" : ISODate("2019-04-22T09:00:00Z"),
            "end_date" : ISODate("2019-04-22T19:12:00Z")
        },
        {
            "start_date" : ISODate("2019-04-23T19:00:00Z"),
            "end_date" : ISODate("2019-04-23T20:59:00Z")
        }
    ]
}
{
    "_id" : ObjectId("5cbeb5e7cf56f3e410320a97"),
    "name" : "sumon",
    "Orders" : [
        {
            "start_date" : ISODate("2019-04-22T13:30:00Z"),
            "end_date" : ISODate("2019-04-22T16:40:00Z")
        },
        {
            "start_date" : ISODate("2019-04-22T15:00:00Z"),
            "end_date" : ISODate("2019-04-22T19:12:00Z")
        },
        {
            "start_date" : ISODate("2019-04-23T19:00:00Z"),
            "end_date" : ISODate("2019-04-23T20:12:00Z")
        }
    ]
}