简体   繁体   English

MongoDB 聚合管道,$gte,$lte?

[英]MongoDB aggregation pipeline, $gte, $lte?

I have a document for software that contain these fields _id, category, brand etc. There is a price field which is of type string.我有一份软件文档,其中包含这些字段 _id、类别、品牌等。有一个字符串类型的价格字段。 Some documents have invalid prices or are null. I want to use an aggregation pipeline so that the price is >=4 and <=8 and convert the price to double.某些文档的价格无效或为 null。我想使用聚合管道,使价格 >=4 且 <=8 并将价格转换为双倍。 There is also a date field that I want to be >=10.还有一个日期字段,我希望它 >=10。 I also want to use $out to create a new collection of this document.我还想使用 $out 创建此文档的新集合。 I have done this so far, I was wondering if someone could let me know how I can retrieve the documents but I don't want to lose or change the other fields only the Price and date.到目前为止我已经这样做了,我想知道是否有人可以让我知道如何检索文件,但我不想丢失或仅更改价格和日期的其他字段。

db.sw.aggregate([{$match: {}}, 
{$project: {priceLen: {"$strLenCP": "$price"}}}, 
{"$match": {priceLen: {"$gte": 4, "$lte": 8}}}, 
{$project: {price: {$trim: {input: "$price", chars: "$"}}}}, 
{$project: {price: {$toDouble: "$price"}}}]) 

my thought process for the $match was to retrieve all the fields.我对 $match 的思考过程是检索所有字段。 Any help will be really appreciated.任何帮助将不胜感激。

No idea what your requirements are in terms of being "correct".不知道您对“正确”的要求是什么。

$project removed all fields (apart from _id ) and populates the given fields. $project删除所有字段(除了_id )并填充给定字段。 If you like to keep existing fields use $set or the alias $addFields which names the actual operation.如果您想保留现有字段,请使用$set或命名实际操作的别名$addFields

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

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