ex1)
//sample
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
{ "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 }
{ "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }
//operation
db.articles.aggregate(
[ { $match : { author : "dave" } } ]
);
//result
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
ex2)
let { title } = req.query;
const regex = (pattern) => new RegExp(`.*${pattern}.*`);
const titleRegex = regex(title);
let posts = await Post.find({ title: { $regex: titleRegex } });
ref) https://docs.mongodb.com/manual/reference/operator/aggregation/match/
+) 또 다른 검색 방법 : $regex 이용
const { title } = req.query;
const existChallenges = await Challenge.find(
{ title: { $regex: `${title}` } },
{ _id: 1, participants: 1, thumbnail: 1, title: 1, startAt: 1 }
).lean(); // populate.._doc..
ref) https://docs.mongodb.com/manual/reference/operator/query/regex/