Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to operate pipe operation in MongoDB

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to operate pipeline exercises in MongoDB, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

$match

Can be used in match, for example, to get all the documents in the collection whose author is "du Fu", as follows:

Db.sang_collect.aggregate ({$match: {author: "du Fu"}})

In practice, it is best to use match as well as index.

Basic usage of $project

$project can be used to extract the desired fields, as follows:

Db.sang_collect.aggregate ({$project: {title:1,_id:0}})

1 means you want the field, 0 means you don't want the field, or you can rename the returned field, such as changing title to articleTitle, as follows:

Db.sang_collect.aggregate ({$project: {"articleTitle": "$title"}})

However, there is a problem to note here. If there is an index on the original field, there will be no index on the renamed field, so it is best to use the index before renaming.

Mathematical expression

Mathematical expressions can be used to add, subtract, multiply and divide a set of values. For example, my data structure is as follows:

{"_ id": ObjectId ("59f841f5b998d8acc7d08863"), "orderAddressL": "ShenZhen", "prodMoney": 45.0, "freight": 13.0, "discounts": 3.0, "orderDate": ISODate ("2017-10-31T09:27:17.342Z"), "prods": ["Coke", "milk tea"]}

The total cost of the order is the cost of goods plus freight. The inquiry is as follows:

Db.sang_collect.aggregate ({$project: {totalMoney: {$add: ["$prodMoney", "$freight"]})

The cost of the actual payment is the total cost less the discount, as follows:

Db.sang_collect.aggregate ({$project: {totalPay: {$subtract: [{$add: ["$prodMoney", "$freight"]}, "$discounts"]}})

Three more nonsense operations, such as calculating the product of prodMoney and freight and discounts:

Db.sang_collect.aggregate ({$project: {test1: {$multiply: ["$prodMoney", "$freight", "$discounts"]})

Another example is to seek the quotient of freight, as follows:

Db.sang_collect.aggregate ({$project: {test1: {$divide: ["$prodMoney", "$freight"]})

Another example is to use prodMoney to take the mold, as follows:

Db.sang_collect.aggregate ({$project: {test1: {$mod: ["$prodMoney", "$freight"]})

Both addition and multiplication can receive multiple parameters, and the rest receive two parameters.

Date expression

The date expression can extract information such as year, month, day, week, hour, minute, second and so on from a date type, as follows:

Db.sang_collect.aggregate ({$project: {"year": {$year: "$orderDate"}, "month": {$month: "$orderDate"}, "week of the year": {$week: "$orderDate"}, "date": {$dayOfMonth: "$orderDate"}, "week": {$dayOfWeek: "$orderDate"}, "days of the year": {$dayOfYear: "$orderDate"}, "hour": {$hour: "$orderDate"}, "points": {$minute: "$orderDate"} "seconds": {$second: "$orderDate"}, "milliseconds": {$millisecond: "$orderDate"}, "Custom formatting time": {$dateToString: {format:% Y% m%% d% H:%M:%S ", date:" $orderDate "})

The implementation results are as follows:

{"_ id": ObjectId ("59f841f5b998d8acc7d08861"), "year": 2017, "month": 10, "week of the year": 44, "date": 31, "week": 3, "Day of the year": 304,304,9, "minutes": 27, "seconds": 17, "milliseconds": 342 "Custom format time": "October 31, 09:27:17, 2017"}

Week says this week is the first week of the year, starting at zero. $dateToString is a feature in MongoDB3.0+. There are several other formatted characters:

String expression

String expressions include string truncation, concatenation, uppercase, lowercase, and other operations. For example, I intercept the first two characters of orderAddressL and return them as follows:

Db.sang_collect.aggregate ({$project: {addr: {$substr: ["$orderAddressL", 0jue 2]})

For example, I stitched orderAddressL and orderDate and returned:

Db.sang_collect.aggregate ({$project: {addr: {$concat: ["$orderAddressL", {$dateToString: {format: "-% Y% m% d", date: "$orderDate"})

The results are as follows:

{"_ id": ObjectId ("59f841f5b998d8acc7d08861"), "addr": "NanJing-- 31 October 2017"}

For example, I convert all orderAddressL to lowercase and return:

Db.sang_collect.aggregate ({$project: {addr: {$toLower: "$orderAddressL"})

For example, I return all the orderAddressL in uppercase:

Db.sang_collect.aggregate ({$project: {addr: {$toUpper: "$orderAddressL"}) logical expression

To compare the size of two numbers, you can use the $cmp operator, as follows:

Db.sang_collect.aggregate ({$project: {test: {$cmp: ["$freight", "$discounts"]})

If the first parameter is greater than the second parameter returns a positive number, and if the first parameter is less than the second parameter, a negative number is returned. You can also use $strcasecmp to compare strings (invalid in Chinese):

Db.sang_collect.aggregate ({$project: {test: {$strcasecmp: [{$dateToString: {format: ".% Y% m / month% d", date: "$orderDate"}}, "$orderAddressL"]})

As for the operators such as ne/gte/lte we introduced earlier, they also apply here. In addition, there are or and and as examples, as follows:

Db.sang_collect.aggregate ({$project: {test: {$and: [{"$eq": ["$freight", "$prodMoney"]}, {"$eq": ["$freight", "$discounts"]}]})

Or means that if one of the parameters returns true,$not if one of the parameters is true, the value of its parameter will be reversed, as follows:

Db.sang_collect.aggregate ({$project: {test: {$not: {"$eq": ["$freight", "$prodMoney"]})

There are also two process control statements, as follows:

Db.sang_collect.aggregate ({$project: {test: {$cond: [false, "trueExpr", "falseExpr"]})

$cond returns trueExpr if the first parameter is true, falseExpr otherwise.

Db.sang_collect.aggregate ({$project: {test: {$ifNull: [null, "replacementExpr"]})

$ifNull returns replacementExpr if the first parameter is null, otherwise the first parameter is returned.

This is the answer to the question about how to operate the pipeline in MongoDB. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report