- 전체
- Sample DB
- database modeling
- [표준 SQL] Standard SQL
- G-SQL
- 10-Min
- ORACLE
- MS SQLserver
- MySQL
- SQLite
- postgreSQL
- 데이터아키텍처전문가 - 국가공인자격
- 데이터 분석 전문가 [ADP]
- [국가공인] SQL 개발자/전문가
- NoSQL
- hadoop
- hadoop eco system
- big data (빅데이터)
- stat(통계) R 언어
- XML DB & XQuery
- spark
- DataBase Tool
- 데이터분석 & 데이터사이언스
- Engineer Quality Management
- [기계학습] machine learning
- 데이터 수집 및 전처리
- 국가기술자격 빅데이터분석기사
- 암호화폐 (비트코인, cryptocurrency, bitcoin)
NoSQL [mongodb] mongodb: how to compare String from document to the String constructed by expression in aggregation pipeline
2020.12.04 15:29
[mongodb] mongodb: how to compare String from document to the String constructed by expression in aggregation pipeline
[Questioin]
I have a trouble with creating a view in MongoDB. I'm just starting with Mongo, so the question could look pretty silly.
I have the following document:
{
"_id" : NumberInt(12213054),
...
"_dt" : {
"_doc" : "time",
"time" : "01:30",
"date" : "04/02/18",
"tz" : "CET",
"tzoffset" : NumberInt(3600),
"uts" : NumberInt(1517704200)
},
...
}
I'm trying to create a view for documents that have a date '04/02/18'. The type of "_dt.date" field is String. And I'm trying to compare it using $eq operator.
I have the following query to select the data:
{$match: {
"_dt.date": {
$eq: {
$concat: [
{$dateToString: {
format: "%d/%m/",
date: new Date("2018-02-04")
}},
{$substr: [
{$year : new Date("2018-02-04")},
2,
2
]}
]
}
}
}
}
But after I execute the query the result is empty.
The date string constructed by the subquery seems to be correct:
db.matches.aggregate({ $project: { date:
{
$concat: [
{$dateToString: {
format: "%d/%m/",
date: new Date("2018-02-04")
}},
{$substr: [
{$year : new Date("2018-02-08")},
2,
2
]}
]
}
}
}
)
Output is:
{
"_id" : NumberInt(12213054),
"date" : "04/02/18"
}
Does anyone have any idea why the first query doesn't work?
[Answer]
$match by design compares a field against a constant value with regular query operators.
For comparing two fields in mongodb you've to use $match with $expr with aggregation query operators in 3.6.
Compare query operators vs aggregation comparison operators.
{"$match":{
"$expr":{
"$eq":[
"$_dt.date",
{"$concat":[
{"$dateToString":{"format":"%d/%m/","date":new Date("2018-02-04")}},
{"$substr":[{"$year":new Date("2018-02-04")},2,2]}
]}
}
}}
Or
You can simply pass the date string created on client side and compare directly
{"$match":{"_dt.date":"04/02/18"}}
[출처] https://stackoverflow.com/questions/48727055/mongodb-how-to-compare-string-from-document-to-the-string-constructed-by-expres
[mongodb] mongodb: how to compare String from document to the String constructed by expression in aggregation pipeline
[Questioin]
I have a trouble with creating a view in MongoDB. I'm just starting with Mongo, so the question could look pretty silly.
I have the following document:
{
"_id" : NumberInt(12213054),
...
"_dt" : {
"_doc" : "time",
"time" : "01:30",
"date" : "04/02/18",
"tz" : "CET",
"tzoffset" : NumberInt(3600),
"uts" : NumberInt(1517704200)
},
...
}
I'm trying to create a view for documents that have a date '04/02/18'. The type of "_dt.date" field is String. And I'm trying to compare it using $eq operator.
I have the following query to select the data:
{$match: {
"_dt.date": {
$eq: {
$concat: [
{$dateToString: {
format: "%d/%m/",
date: new Date("2018-02-04")
}},
{$substr: [
{$year : new Date("2018-02-04")},
2,
2
]}
]
}
}
}
}
But after I execute the query the result is empty.
The date string constructed by the subquery seems to be correct:
db.matches.aggregate({ $project: { date:
{
$concat: [
{$dateToString: {
format: "%d/%m/",
date: new Date("2018-02-04")
}},
{$substr: [
{$year : new Date("2018-02-08")},
2,
2
]}
]
}
}
}
)
Output is:
{
"_id" : NumberInt(12213054),
"date" : "04/02/18"
}
Does anyone have any idea why the first query doesn't work?
[Answer]
$match by design compares a field against a constant value with regular query operators.
For comparing two fields in mongodb you've to use $match with $expr with aggregation query operators in 3.6.
Compare query operators vs aggregation comparison operators.
{"$match":{
"$expr":{
"$eq":[
"$_dt.date",
{"$concat":[
{"$dateToString":{"format":"%d/%m/","date":new Date("2018-02-04")}},
{"$substr":[{"$year":new Date("2018-02-04")},2,2]}
]}
}
}}
Or
You can simply pass the date string created on client side and compare directly
{"$match":{"_dt.date":"04/02/18"}}
[출처] https://stackoverflow.com/questions/48727055/mongodb-how-to-compare-string-from-document-to-the-string-constructed-by-expres
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 공지 | 오라클 기본 샘플 데이터베이스 | 졸리운_곰 | 2014.01.02 | 86258 |
| 공지 | [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE | 가을의 곰을... | 2013.02.10 | 78724 |
| 공지 | [G_SQL] Sample Database | 가을의 곰을... | 2012.05.20 | 95468 |

