マングースクエリパターン
17571 ワード
マングースクエリはまた、MongoDBに関連する関数です.あなたがMongoDBからデータを取得する場合は、マングースを使用することができます.
マングースには異なる種類のクエリがあります.あなたはmongoose文書のすべての詳細を見つけることができます.Link
ほとんどの初心者はマングースのcrud機能について知っている.今日、私はCRUD機能について言いません.少し違う質問をしようとします.この質問が好きです.始めましょう.
私はまた、MongoDBデータベースを接続する方法を知ってほしい.あなたが知らないならば、あなたはこの記事を読むことができます.
ステップ1
このプロジェクトをダウンロードしてコンピュータにインストールします.link
ステップ2
モデルについての最初の話私たちは5つのモデル[ src/model ]を持っています.これは
1 -ユーザ-名前、電子メール、電話
2 -製品-それは、カテゴリのコレクションとサブカテゴリのコレクションとShopNameによってsubcatid参照で名前、catidの参照を持っています.
カテゴリー-それは名前だけです.
サブカテゴリ-それはカテゴリのコレクションによって名前とcatid参照を持っています.
請求書-ユーザコレクションとプロダクションIDによるユーザID参照
製品別参照.
ダミーデータに関連するダミーモデルを含む.ちょうどあなたのMongoDBデータベースにこのデータをインポートします.
ステップ3
オープンProductController.JSファイル[ src/controller/productcontroller . js ].最初にこの機能を話しています
このクエリは、すべての製品情報をMongoDBから取得します.しかし、私はあなたがこの出力を満足していない知っています.catidとsubcatidが完全な詳細を示していないので.それで、我々は何をしますか.第2の機能に行きましょう
現在、我々はcatidとsubcatidの詳細を見ることができます.
私は、あなたがこの結果を見て満足していると思います.しかし、私はそうではありません.もっと欲しい.同じように、ページにしたいです.よし、やろう
最初に、「Mongoose - Aggregate - Paginite - V 2」プラグインが必要になります.NPMサイトを見つけることができます.次に、このプラグインを製品モデルに追加します.
再びProductControllerに行き、このコードを書きます
また、ページネーションについても知っています.今、私たちは少し厳しい質問に行きます.仮に、お店の名産品やトータルショップの下にもお店が欲しいと思います.次に、あなたは何をします.単純にこのコードを書きます
我々が見るもう一つの最後の質問.を呼び出します.JSファイル[ src/controller/invoicecontroller . js ]とこのコードを参照してください.
それは完全にゴミの出力です.適切な出力を表示しない.この出力をクリアしましょう
今、いくつかの残りを取るし、あなたの脳をリフレッシュします.もう一つの日我々は、詳細をmongoose質問を話します.
[それが役に立つなら、リポジトリにスターを渡してください.😇]
https://github.com/kamruzzamanripon/mongoose-query-pattern
マングースには異なる種類のクエリがあります.あなたはmongoose文書のすべての詳細を見つけることができます.Link
ほとんどの初心者はマングースのcrud機能について知っている.今日、私はCRUD機能について言いません.少し違う質問をしようとします.この質問が好きです.始めましょう.
私はまた、MongoDBデータベースを接続する方法を知ってほしい.あなたが知らないならば、あなたはこの記事を読むことができます.
ステップ1
このプロジェクトをダウンロードしてコンピュータにインストールします.link
ステップ2
モデルについての最初の話私たちは5つのモデル[ src/model ]を持っています.これは
1 -ユーザ-名前、電子メール、電話
2 -製品-それは、カテゴリのコレクションとサブカテゴリのコレクションとShopNameによってsubcatid参照で名前、catidの参照を持っています.
カテゴリー-それは名前だけです.
サブカテゴリ-それはカテゴリのコレクションによって名前とcatid参照を持っています.
請求書-ユーザコレクションとプロダクションIDによるユーザID参照
製品別参照.
ダミーデータに関連するダミーモデルを含む.ちょうどあなたのMongoDBデータベースにこのデータをインポートします.
ステップ3
オープンProductController.JSファイル[ src/controller/productcontroller . js ].最初にこの機能を話しています
static allProductWithOutPagination = async(req, res)=>{
try{
const allProductInfo = await Product.find().exec();
return res.status(200).json({
code: 200,
message: "Product List",
data: allProductInfo,
});
}catch(error){
res.status(501).json({
code: 501,
message: error.message,
error: true,
});
}
}
URLがhttp://localhost:5000/api/v1/all-userであるあなたの郵便配達人でこの機能を走らせるならば.この出力が表示されます-このクエリは、すべての製品情報をMongoDBから取得します.しかし、私はあなたがこの出力を満足していない知っています.catidとsubcatidが完全な詳細を示していないので.それで、我々は何をしますか.第2の機能に行きましょう
static allProductWithOutPaginationAndJoinCollection = async(req, res)=>{
try{
//const Users = await Product.find().exec();
const allProductInfo = await Product.aggregate([
{$lookup:
{
from: "categories", // collection to join
localField: "catId", //this field as refereence for category collection id
foreignField: "_id", //this field is category id to join product collecton on base one catId === _id;
as: "categoryInfo" // output array field
}
},
{ $unwind:"$categoryInfo" },
{
$lookup: {
from: "subCategories",
localField: "subCatId",
foreignField: "_id",
as: "subCategoryInfo"
}
},
{ $unwind:"$subCategoryInfo" },
//For presentation
{
$project: {
"_id": 1,
"name": 1,
"shopName": 1,
"categoryInfo._id": 1,
"categoryInfo.name": 1,
"subCategoryInfo._id": 1,
"subCategoryInfo.name": 1,
}
}
]
).exec();
return res.status(200).json({
code: 200,
message: "Product List",
data: allProductInfo,
});
}catch(error){
res.status(501).json({
code: 501,
message: error.message,
error: true,
});
}
}
URLがhttp://localhost:5000/api/v1/all-product-without-paination-join-collectionであるあなたの郵便配達人でこの機能を走らせるならば.この出力が表示されます-現在、我々はcatidとsubcatidの詳細を見ることができます.
私は、あなたがこの結果を見て満足していると思います.しかし、私はそうではありません.もっと欲しい.同じように、ページにしたいです.よし、やろう
最初に、「Mongoose - Aggregate - Paginite - V 2」プラグインが必要になります.NPMサイトを見つけることができます.次に、このプラグインを製品モデルに追加します.
再びProductControllerに行き、このコードを書きます
static allProductWithPaginationAndJoinCollection = async(req, res)=>{
try{
//const Users = await Product.find().exec();
const options = {
page: 1,
limit: 10,
};
const allProductInfo = Product.aggregate([
{$lookup:
{
from: "categories", // collection to join
localField: "catId", //this field as refereence for category collection id
foreignField: "_id", //this field is category id to join product collecton on base one catId === _id;
as: "categoryInfo" // output array field
}
},
{ $unwind:"$categoryInfo" },
{
$lookup: {
from: "subCategories",
localField: "subCatId",
foreignField: "_id",
as: "subCategoryInfo"
}
},
{ $unwind:"$subCategoryInfo" },
//For presentaiton
{
$project: {
"_id": 1,
"name": 1,
"shopName": 1,
"categoryInfo._id": 1,
"categoryInfo.name": 1,
"subCategoryInfo._id": 1,
"subCategoryInfo.name": 1,
}
}
]
)
const productInfoAddPagination = await Product.aggregatePaginate(allProductInfo, options);
//return console.log(productInfoAddPagination)
return res.status(200).json({
code: 200,
message: "Product List with pagination",
data: productInfoAddPagination,
});
}catch(error){
res.status(501).json({
code: 501,
message: error.message,
error: true,
});
}
}
URLがhttp://localhost:5000/api/v1/all-product-with-paination-join-collectionであるあなたの郵便配達人でこの機能を走らせるならば.この出力が表示されます-また、ページネーションについても知っています.今、私たちは少し厳しい質問に行きます.仮に、お店の名産品やトータルショップの下にもお店が欲しいと思います.次に、あなたは何をします.単純にこのコードを書きます
static groupWiseProductInfoWithPaginationAndJoinCollection = async(req, res)=>{
try{
const allProductInfo = await Product.aggregate([
{$lookup:
{
from: "categories", // collection to join
localField: "catId", //this field as refereence for category collection id
foreignField: "_id", //this field is category id to join product collecton on base one catId === _id;
as: "categoryInfo" // output array field
}
},
{ $unwind:"$categoryInfo" },
{
$lookup: {
from: "subCategories",
localField: "subCatId",
foreignField: "_id",
as: "subCategoryInfo"
}
},
{ $unwind:"$subCategoryInfo" },
//For presentaiton
{
$project: {
"_id": 1,
"name": 1,
"shopName": 1,
"categoryInfo._id": 1,
"categoryInfo.name": 1,
"subCategoryInfo._id": 1,
"subCategoryInfo.name": 1,
}
},
{
$group: {
_id: { shopName: "$shopName" },
count: {$sum: 1},
details: { $push: '$$ROOT' },
},
}
]
).exec();
return res.status(200).json({
code: 200,
message: "Group wise Product List",
data: allProductInfo,
});
}catch(error){
res.status(501).json({
code: 501,
message: error.message,
error: true,
});
}
}
URLがhttp://localhost:5000/api/v1/group-wise-product-with-paination-join-collectionであるあなたの郵便配達人でこの機能を走らせるならば.この出力が表示されます-{
"_id": {
"shopName": "Effertz"
},
"count": 12,
"details": [
{
"_id": "625b1961a45d3893f8b23191",
"name": "Treeflex",
"shopName": "Effertz",
"categoryInfo": {
"_id": "625b0e6aa45d3893f8b2308f",
"name": "Zamit"
},
"subCategoryInfo": {
"_id": "625b117da45d3893f8b230c5",
"name": "Naturasil"
}
},
{
"_id": "625b1961a45d3893f8b23192",
"name": "Overhold",
"shopName": "Effertz",
"categoryInfo": {
"_id": "625b0e6aa45d3893f8b2308b",
"name": "Y-find"
},
"subCategoryInfo": {
"_id": "625b117da45d3893f8b230cf",
"name": "Loratadine"
}
}
今、あなたはマングースのクエリの力を参照してください.本当にすごい.我々が見るもう一つの最後の質問.を呼び出します.JSファイル[ src/controller/invoicecontroller . js ]とこのコードを参照してください.
static invoiceAll = async(req, res)=>{
try{
const invoiceAll = await Invoice.find().exec();
return res.status(200).json({
code: 200,
message: "All invoice",
data: invoiceAll,
});
}catch(error){
res.status(501).json({
code: 501,
message: error.message,
error: true,
});
}
}
URLはhttp://localhost:5000/api/v1/invoice-allの郵便番号にこの関数を実行します.この出力が表示されます-それは完全にゴミの出力です.適切な出力を表示しない.この出力をクリアしましょう
static invoiceAllInfoDetails = async(req, res)=>{
try{
const invoiceAllInfoDetails = await await Invoice.aggregate([
{$lookup:
//join users collection
{
from: "users", // collection to join
localField: "userId", //this field as refereence for User collection id
foreignField: "_id", //this field is category id to join product collecton on base one catId === _id;
as: "userInfo" // output array field
}
},
//join products collection
{$lookup: {
from: "products",
let: { "products": "$productId" },
pipeline: [
{ $match: { $expr: { $in: [ "$_id", "$$products" ] } } }
],
as: "productInfo"
}
},
//join categories collection
{$lookup:
{
from: "categories",
localField: "productInfo.catId",
foreignField: "_id",
as: "categoryInfo"
}
},
//join subCategory collection
{$lookup:
{
from: "subCategories",
localField: "productInfo.subCatId",
foreignField: "_id",
as: "subCategoryInfo"
}
}
]
).exec();
return res.status(200).json({
code: 200,
message: "All invoice with details information",
data: invoiceAllInfoDetails,
});
}catch(error){
res.status(501).json({
code: 501,
message: error.message,
error: true,
});
}
}
URLはhttp://localhost:5000/api/v1/invoice-all-with-details-infoの郵便番号にこの関数を実行します.この出力が表示されます-{
"_id": "625c7ab23ab63761bf85b143",
"userId": "625b0dd5a45d3893f8b2304b",
"productId": [
"625b1961a45d3893f8b230e7",
"625b1961a45d3893f8b230e8"
],
"date": "3/12/2022",
"userInfo": [
{
"_id": "625b0dd5a45d3893f8b2304b",
"name": "Tú",
"email": "[email protected]",
"phone": "260-756-6184"
}
],
"productInfo": [
{
"_id": "625b1961a45d3893f8b230e7",
"name": "Kanlam",
"catId": "625b0e6aa45d3893f8b2307f",
"subCatId": "625b117da45d3893f8b230a6",
"shopName": "Kling"
},
{
"_id": "625b1961a45d3893f8b230e8",
"name": "Fix San",
"catId": "625b0e6aa45d3893f8b2307f",
"subCatId": "625b117da45d3893f8b230a6",
"shopName": "Kling"
}
],
"categoryInfo": [
{
"_id": "625b0e6aa45d3893f8b2307f",
"name": "Sonair"
}
],
"subCategoryInfo": [
{
"_id": "625b117da45d3893f8b230a6",
"name": "Metronidazole",
"catId": "625b0e6aa45d3893f8b2307f"
}
]
}
今、いくつかの残りを取るし、あなたの脳をリフレッシュします.もう一つの日我々は、詳細をmongoose質問を話します.
[それが役に立つなら、リポジトリにスターを渡してください.😇]
https://github.com/kamruzzamanripon/mongoose-query-pattern
Reference
この問題について(マングースクエリパターン), 我々は、より多くの情報をここで見つけました https://dev.to/kamruzzaman/mongoose-query-pattern-2p21テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol