Cara menggunakan mongodb cond in array

This is the case when we have two or more conditions and, as a result, we get documents in which one element of the array matches one condition and another document only matches the other condition.

Show

    Of course, what we want is that one element of the array matches both conditions at the same time.

    In this tutorial, we are going to learn how to select only those documents in our dataset that contain fields within arrays that satisfy our requirements, and project only the array’s fields we need.

    The MongoDB client Studio 3T provides multiple ways to query MongoDB arrays.

    This tutorial covers how to filter elements using the

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    6 operator alongside
    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    7, the MongoDB aggregation stages
    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    8 and
    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    9, and IntelliShell, Studio 3T’s built-in mongo shell.

    If you would prefer a no-code, visual approach, here’s a tutorial on how to query string values and array elements using a drag-and-drop query builder.

    In this post, the dataset we are going to work with consists only in a collection of items called

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    0 and two documents – enough for our didactic purposes.

    As is already habitual in my previous posts, the database we are going to use is called

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    1.

    The info we have in each document is:

    • BulkWriteResult({
      	"writeErrors" : [ ],
      	"writeConcernErrors" : [ ],
      	"nInserted" : 2,
      	"nUpserted" : 0,
      	"nMatched" : 0,
      	"nModified" : 0,
      	"nRemoved" : 0,
      	"upserted" : [ ]
      })
      2 – Code of the article
    • BulkWriteResult({
      	"writeErrors" : [ ],
      	"writeConcernErrors" : [ ],
      	"nInserted" : 2,
      	"nUpserted" : 0,
      	"nMatched" : 0,
      	"nModified" : 0,
      	"nRemoved" : 0,
      	"upserted" : [ ]
      })
      3 – Article description
    • BulkWriteResult({
      	"writeErrors" : [ ],
      	"writeConcernErrors" : [ ],
      	"nInserted" : 2,
      	"nUpserted" : 0,
      	"nMatched" : 0,
      	"nModified" : 0,
      	"nRemoved" : 0,
      	"upserted" : [ ]
      })
      4 – Array with information regarding the purchases made to our providers
    •  
      BulkWriteResult({
      	"writeErrors" : [ ],
      	"writeConcernErrors" : [ ],
      	"nInserted" : 2,
      	"nUpserted" : 0,
      	"nMatched" : 0,
      	"nModified" : 0,
      	"nRemoved" : 0,
      	"upserted" : [ ]
      })
      5 – Array with information regarding the stock per country and warehouse

    We’ll be using IntelliShell, the built-in mongo shell in Studio 3T, throughout the tutorial.  Let’s use it to add our two new documents.

    1. .

    2. .

    3. Open IntelliShell, done quickest with the keyboard shortcut Ctrl + L (⌘+ L).

    Get IntelliShell in the latest Studio 3T version.

    4. Execute this query in our

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    1 database in order to store these two documents in our
    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    0 collection:

    db.articles.insert([
    {
    	"_id" : 1,
    	"description" : "DESCRIPTION ARTICLE AB",
    	"article_code" : "AB",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("80.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("85.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("79.910000")
    		}
    	],
    	"stock" : [
    	    {
    	        "country" : "01",
    	        "warehouse" : {
    	            "code" : "02",
    	            "units" : 10
    	        }
    	    },
    	    {
    	        "country" : "02",
    	        "warehouse" : {
    	            "code" : "02",
    	            "units" : 8
    	        }
    	    }
    	]
    },
    {
    	"_id" : 2,
    	"description" : "DESCRIPTION ARTICLE AC",
    	"article_code" : "AC",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("90.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("95.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("89.910000")
    		}
    	],
    	"stock" : [
    	    {
    	        "country" : "01",
    	        "warehouse" : {
    	            "code" : "01",
    	            "units" : 20
    	        }
    	    },
    	    {
    	        "country" : "02",
    	        "warehouse" : {
    	            "code" : "02",
    	            "units" : 28
    	        }
    	    }
    	]
    }
    ]);
    

    5. In the Result tab, choose JSON View. The result should be:

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })

    Now that we’ve inserted the two documents, suppose we want to get those items with stock in the warehouse

    db.articles.find({
        "stock.country" : "01",
        "stock.warehouse.code" : "02"
    }).pretty();
    
    0 of the country
    db.articles.find({
        "stock.country" : "01",
        "stock.warehouse.code" : "02"
    }).pretty();
    
    1.

    If we do not have the necessary experience, our first try would be something like this:

    db.articles.find({
        "stock.country" : "01",
        "stock.warehouse.code" : "02"
    }).pretty();
    

    Which gives us this result in JSON View:

    {
    	"_id" : 1,
    	"description" : "DESCRIPTION ARTICLE AB",
    	"article_code" : "AB",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("80.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("85.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("79.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 10
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 8
    			}
    		}
    	]
    }
    {
    	"_id" : 2,
    	"description" : "DESCRIPTION ARTICLE AC",
    	"article_code" : "AC",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("90.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("95.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("89.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "01",
    				"units" : 20
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 28
    			}
    		}
    	]
    }
    

    Side note: It is very easy to build such

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    7 queries using the Visual Query Builder in Studio 3T .

    Related reading: Visual Query Builder, Studio 3T’s drag-and-drop MongoDB query builder

    Cara menggunakan mongodb cond in array

    In the screenshot above, we’ve simply dragged the fields into the query builder, defined the values, and run the query.

    The query returned the same two documents, pasted again in whole below.

    {
    	"_id" : 1,
    	"description" : "DESCRIPTION ARTICLE AB",
    	"article_code" : "AB",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("80.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("85.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("79.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 10
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 8
    			}
    		}
    	]
    }
    {
    	"_id" : 2,
    	"description" : "DESCRIPTION ARTICLE AC",
    	"article_code" : "AC",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("90.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("95.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("89.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "01",
    				"units" : 20
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 28
    			}
    		}
    	]
    }
    

    If we look carefully at the document where

    db.articles.find({
        "stock.country" : "01",
        "stock.warehouse.code" : "02"
    }).pretty();
    
    3 field equals
    db.articles.find({
        "stock.country" : "01",
        "stock.warehouse.code" : "02"
    }).pretty();
    
    4 (bolded above) our requirements are fulfilled in one of the subdocuments of the array.

    In the document where

    db.articles.find({
        "stock.country" : "01",
        "stock.warehouse.code" : "02"
    }).pretty();
    
    3 field equals
    db.articles.find({
        "stock.country" : "01",
        "stock.warehouse.code" : "02"
    }).pretty();
    
    6 (also bolded above), only one condition is matched in each of the subdocuments of the array.

    Our purpose is to obtain only those documents in which all of our conditions are fulfilled, at the same time, in the subdocument, or subdocuments, of the array.

    That means this query should not return the document which

    db.articles.find({
        "stock.country" : "01",
        "stock.warehouse.code" : "02"
    }).pretty();
    
    3 field equals
    db.articles.find({
        "stock.country" : "01",
        "stock.warehouse.code" : "02"
    }).pretty();
    
    6.

    In order to build this query in a proper way, we must use the

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    6 operator.

    The official MongoDB documentation for version 4.0 states:

    The

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    6 operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

    This is the syntax of the

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    6 operator:

    <fieldName> : { $elemMatch : { <condition1>, <condition2>, … } }

    We can use the

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    6 in the query part of the
    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    7 function, and/or in the projection part of it.

    Let’s go back to IntelliShell and run this query:

    db.articles.find(
      { stock : { $elemMatch : { country : "01", "warehouse.code" : "02" } } }
    ).pretty();

    This should give you the result:

    {
    	"_id" : 1,
    	"description" : "DESCRIPTION ARTICLE AB",
    	"article_code" : "AB",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("80.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("85.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("79.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 10
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 8
    			}
    		}
    	]
    }
    

    As we expected, the query has returned the right document (where

    db.articles.find({
        "stock.country" : "01",
        "stock.warehouse.code" : "02"
    }).pretty();
    
    3 field equals
    db.articles.find({
        "stock.country" : "01",
        "stock.warehouse.code" : "02"
    }).pretty();
    
    4).

    Next, we will learn how to filter out of the array only those elements that meet our criteria using the aggregation stage,

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    9.

    To make writing the aggregation query easier, we will use Studio 3T’s Aggregation Editor.

    Let’s get started by taking our original query and copying only the bolded part:

    db.articles.find(
      { stock : { $elemMatch : { country : "01", "warehouse.code" : "02" } } }
    ).pretty();

    Open Aggregation Editor in Studio 3T, done quickest with the keyboard shortcut F4.

    Add a first stage using the

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    8 operator and paste the bolded part of the query. Make sure you remove any existing placeholder brackets or you might get an error.

    Execute the pipeline. The result should look like the screenshot below, in JSON View.

    Cara menggunakan mongodb cond in array

    Related reading: Aggregation Editor, Studio 3T’s stage-by-stage MongoDB aggregation pipeline builder.

    On several occasions, we do not want to project (or display) all the fields in the document, but only those that match our requirements.

    For this, we need to filter out of the array only those elements that meet our criteria. We can do this using the

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    9 aggregation stage.

    To achieve our goal – to filter the document to show only the fields we need – we must use the

    {
    	"_id" : 1,
    	"description" : "DESCRIPTION ARTICLE AB",
    	"article_code" : "AB",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("80.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("85.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("79.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 10
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 8
    			}
    		}
    	]
    }
    {
    	"_id" : 2,
    	"description" : "DESCRIPTION ARTICLE AC",
    	"article_code" : "AC",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("90.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("95.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("89.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "01",
    				"units" : 20
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 28
    			}
    		}
    	]
    }
    
    9 operator alongside the aggregation framework.

    Let’s continue with the same example.

    We maintain the

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    8 stage to get only the document we need and then we add the
    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    9 stage.

    Inside this stage, we project all the fields we need and, through the

    {
    	"_id" : 1,
    	"description" : "DESCRIPTION ARTICLE AB",
    	"article_code" : "AB",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("80.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("85.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("79.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 10
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 8
    			}
    		}
    	]
    }
    {
    	"_id" : 2,
    	"description" : "DESCRIPTION ARTICLE AC",
    	"article_code" : "AC",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("90.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("95.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("89.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "01",
    				"units" : 20
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 28
    			}
    		}
    	]
    }
    
    9 operator, we tell the database which fields inside the array we want to project, too.

    The

    {
    	"_id" : 1,
    	"description" : "DESCRIPTION ARTICLE AB",
    	"article_code" : "AB",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("80.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("85.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("79.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 10
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 8
    			}
    		}
    	]
    }
    {
    	"_id" : 2,
    	"description" : "DESCRIPTION ARTICLE AC",
    	"article_code" : "AC",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("90.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("95.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("89.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "01",
    				"units" : 20
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 28
    			}
    		}
    	]
    }
    
    9 operator has three variables:

    • input: the array we are going to work with
    • as: reference name
    • cond: our requirements

    Right now this is how our query looks like in full. Let’s copy, paste, and run the query in IntelliShell.

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    0

    Then, let’s check whether the output responds to what we are looking for:

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    1

    Ok, we got it!

    Now back to the Aggregation Editor.

    Let’s add a second stage using the

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    9 operator and paste the query below. Make sure you remove any existing placeholder brackets or you might get an error.

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    2

    Next, execute the full pipeline. You should see only one document.

    Cara menggunakan mongodb cond in array

    Maybe you’re wondering: Why don’t we use the

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    6 operator in the projection part of the
    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    7, instead of using the
    {
    	"_id" : 1,
    	"description" : "DESCRIPTION ARTICLE AB",
    	"article_code" : "AB",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("80.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("85.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("79.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 10
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 8
    			}
    		}
    	]
    }
    {
    	"_id" : 2,
    	"description" : "DESCRIPTION ARTICLE AC",
    	"article_code" : "AC",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("90.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("95.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("89.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "01",
    				"units" : 20
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 28
    			}
    		}
    	]
    }
    
    9 operator in the aggregation framework?

    Let’s refer to the official documentation:

    The

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    6 operator projects the first matching element from an array based on a condition.

    OK. In order to better understand what this means, we are going to insert a new document in our

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    0 collection.

    Go back to IntelliShell and run the query below to insert our third document.

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    3

    Next, let’s run this query using the

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    6 operator in the projection part of the
    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    7 function.

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    4

    This is the document the query returns:

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    5

    Thus, this is not what we are looking for and this is the reason we do not use it.

    We’ve learned how to resolve one of the most common problems that commonly plagues those new to MongoDB, which is how to query an array when more than one condition must be fulfilled in the same element.

    We now also know how to use the

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    6 operator.

    Once we achieved our first goal, the second was to extract from the array only the elements we needed. Now we know and are able to use the

    {
    	"_id" : 1,
    	"description" : "DESCRIPTION ARTICLE AB",
    	"article_code" : "AB",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("80.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("85.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("79.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 10
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 8
    			}
    		}
    	]
    }
    {
    	"_id" : 2,
    	"description" : "DESCRIPTION ARTICLE AC",
    	"article_code" : "AC",
    	"purchase" : [
    		{
    			"company" : 1,
    			"cost" : NumberDecimal("90.010000")
    		},
    		{
    			"company" : 2,
    			"cost" : NumberDecimal("95.820000")
    		},
    		{
    			"company" : 3,
    			"cost" : NumberDecimal("89.910000")
    		}
    	],
    	"stock" : [
    		{
    			"country" : "01",
    			"warehouse" : {
    				"code" : "01",
    				"units" : 20
    			}
    		},
    		{
    			"country" : "02",
    			"warehouse" : {
    				"code" : "02",
    				"units" : 28
    			}
    		}
    	]
    }
    
    9 operator alongside the aggregation framework.

    Finally, we’ve learned about the use of the

    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
    8 operator in the project part of the find function.

    Find more exercises to practice your array-filtering skills in our MongoDB course, MongoDB 201: Querying MongoDB Data. Dive straight in, no registration needed.