db.students.find( { grades: { $elemMatch: {
mean: { $gt: 70 },
grade: { $gt:90 }
} } },
{ "grades.$": 1 } )
this is on official mongodb document, I have troubling translate them into mongoengine clause.
Can anybody translate this mongodb clause into mongoengine clause?
Because I have a embedded document in an array, I need to filter something inside the embedded document, I found this on the document, but don't know the exactly way to translate it. thank you!
I got the answer, but I do it another way around, I implement elasticsearch into my mongodb to be the search engine, so that's not the problem
Related
Use case
Adding a field to a specific object in an array of objects using updateOne during a bulkUpdateOps
Blockers
I have been unable to find a way to identify and update a specific object in the subdocument array of a specific record.
I only have access to this DB through MongoDB Compass, and I plan to use the provided mongosh tool.
Data example
Our purchaseorders model looks like this:
{
_id: uuid,
...rest,
documents:[
{
_id:uuid,
forignKey2:string (optional),
keyIWantToAdd:string (optional),
...rest
}
]
}
So if I have
{
_id:*1,
documents:[
{
_id:*2,
forignKey2:'no-test',
...rest
},
{
_id:*3,
forignKey2:'test',
...rest
},
]
}
I want to add a key and value like this (really I'm willing to do anything to set these values, this is just the closest I have been able to get):
var bulkUpdateOps = db.purchaseorders.initializeOrderedBulkOp();
bulkUpdateOps.find('*1').updateOne({
$set:{
documents.[index of document object with forignKey2:'test' or _id:*3 whichever is easier].keyIWantToAdd:'valueIWantToAdd'
}
})
bulkUpdateOps.execute();
Any help or suggestions would be greatly appreciated.
#rickhg12hs posted exactly what I was looking for. For anyone else using mongodb.com/docs/manual/reference/method/Bulk.find.arrayFilters the bulk find is being ran on an array of objects like this: { grades:[ { grade: 85, mean: number } ] }
I have a field stored in a mongo collection that is an integer bitmask, and I frequently use the $bitsAllSet query operator to find documents where certain bit flags are set.
Now I would like to do an aggregation to get counts of documents for each bit flag. I figured I would start by trying to project if the bit is set or not.
Something like..
db.MyCollection.aggregate(
[
{ $project: {
bit1: { $bitsAllSet: ["$myBitMask", 1] },
bit2: { $bitsAllSet: ["$myBitMask", 2] },
bit3: { $bitsAllSet: ["$myBitMask", 4] },
bit4: { $bitsAllSet: ["$myBitMask", 8] }
} }
])
but this is invalid syntax and I cannot find anything in the mongo documentation that looks like it would help me accomplish this.
Is there a way achieve this?
We are using MongoDB Atlas Search Indexes to query and filter the documents list. consider below document.
{_id:5sfgdfgf2c46e0fb00019bdecc,
user_id:4996,
bom_number:"112",
name:"test"}
Created default Index:
{
"mappings": {
"dynamic": false,
"fields": {
"user_id": {
"type": "string"
}
}
}
}
Query:
{
text: {
query: ["4996","3888"],
path: 'user_id'
}
}
It is not returning any documents. Can anyone please guide. I know we actually stored user_id as numeric form and I tried indexing and querying the field as String. Please help me out and suggest correct way of querying mongo atlas numeric field with multiple values.
$search is what you need to use to query using the index that you have created and you also need to mention the name of the index in the query.
The below draft might help you:
$search:{
{
index: "index_name_created_from_atlas_search",
text: {
query: "<actual data you need to search>",
path: "<field_name>"
}
}
}
And based on your example, if you want to have multiple conditions with the search, you can refer to official documentation here of all sorts of multiple conditions you can integrate with the $search aggregation using the compound operator.
{
questions: {
q1: "",
}
}
result after updating:
{
questions: {
q1: "",
q2: ""
}
}
I want to add q2 inside questions, without overwritting what's already inside it (q1).
One solution I found is to get the whole document, and modify it on my backend, and send the whole document to replace the current one. But it seems really in-effiecient as I have other fields in the document as well.
Is there a query that does it more efficiently? I looked at the mongodb docs, didn't seem to find a query that does it.
As turivishal said, you can use $set like this
But you also can use $addFields in this way:
db.collection.aggregate([
{
"$match": {
"questions.q1": "q1"
}
},
{
"$addFields": {
"questions.q2": "q2"
}
}
])
Example here
Also, reading your comment where you say Cannot create field 'q2' in element {questions: "q1"}. It seems your original schema is not the same you have into your DB.
Your schema says that questions is an object with field q1. But your error says that questions is the field and q1 the value.
I'm pretty new to ElasticSearch, I recently indexed one of my MongoDB collection on ElasticSearch using compose transporter. The reason I indexed is I wanted to do full-text search on some of the fields.
So, now I have a nested document like below,
{
"item":"journal",
"item_size":200,
"instock":[
{
"warehouse":"A",
"qty":5
},
{
"warehouse":"A",
"qty":15
}
]
}
If I want to do a MongoDB aggregate on the doc to match where warehouse is A and $unwind them,
[{"$unwind":"$instock"},{"$match":{"instock.warehouse":"A"}}]
I'll get the following result,
{
"item":"journal",
"item_size":200,
"instock":
{
"warehouse":"A",
"qty":5
}
}
{
"item":"journal",
"item_size":200,
"instock":
{
"warehouse":"A",
"qty":15
}
}
This is just a simple example but I have more nested complex documents like this in MongoDB. How can I achieve the same result in the ElasticSearch?
I spent a lot of time figuring out this but no luck.