i am using mongodb 2.6
my schema
{
list:[
{ Sno:1,
sublist:[
{
subid:"1"
dats:"result"
},
{
subid:"2"
dats:"result"
}
]
},
{ Sno:2,
sublist:[
{
subid:"3"
dats:"result"
},
{
subid:"4"
dats:"result"
}
]
}
]
}
db.test.find({"list.sublist.subid": 2});
i want to return only matched data like this
i don't know how to use mapreduce for this solution
{
sublist:[
{
subid:"1"
dats:"result"
}
]
}
You will need to use a projection http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/
In your case try with $elemMatch (http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/#proj._S_elemMatch)
db.test.find( {},
{ sublist: { $elemMatch: { subid: "1" } } } )
Related
I have been trying to scan DynamoDB to check for particular value in a nested map attribute named deliverables. However using scan with filter expressions is resulting in an empty result.
import boto3
result = []
dynamo_client = boto3.client("dynamodb")
paginator = dynamo_client.get_paginator("scan")
operation_parameters = {
'FilterExpression': "#Deliverable= :deliverable",
'ExpressionAttributeNames': {
'#Deliverable': 'deliverables.fc986523-a666-478e-8303-2a1c3c1dc4ba'
},
'ExpressionAttributeValues': {
':deliverable': {
"M": {
"read": {
"BOOL": True
},
"upload": {
"BOOL": True
},
"write": {
"BOOL": True
}
}
}
}
}
for page in paginator.paginate(TableName="TableName", **operation_parameters):
result.append(page["Items"])
print(result)
The items in the dynamo db look like this:
[
[
{
"deliverables":{
"M":{
"7397d832-fefb-4ba2-97a1-0f6e73d611d9":{
"M":{
"read":{
"BOOL":true
},
"upload":{
"BOOL":true
},
"write":{
"BOOL":true
}
}
},
"fc986523-a666-478e-8303-2a1c3c1dc4ba":{
"M":{
"read":{
"BOOL":true
},
"upload":{
"BOOL":true
},
"write":{
"BOOL":true
}
}
}
}
},
"username":{
"S":"username1"
},
"deniedReferences":{
"L":[
]
}
},
{
"deliverables":{
"M":{
"7397d832-fefb-4ba2-97a1-0f6e73d611d9":{
"M":{
"read":{
"BOOL":true
},
"upload":{
"BOOL":false
},
"write":{
"BOOL":false
}
}
},
"fc986523-a666-478e-8303-2a1c3c1dc4ba":{
"M":{
"read":{
"BOOL":true
},
"upload":{
"BOOL":false
},
"write":{
"BOOL":false
}
}
}
}
},
"username":{
"S":"repositoryadmin"
},
"deniedReferences":{
"L":[
]
}
}
]
]
Please let me know if you can help me solve this issue.
The problem is the [dot] here: 'ExpressionAttributeNames': { '#Deliverable': 'deliverables.fc986523-a666-478e-8303-2a1c3c1dc4ba'}
Expressions docs: DynamoDB interprets a dot in an expression attribute name as a character within an attribute's name.
operation_parameters = {
"FilterExpression": "#D0.#D1=:deliverable", # the dot goes here!
"ExpressionAttributeNames": {
"#D0": "deliverables",
"#D1": "fc986523-a666-478e-8303-2a1c3c1dc4ba"
},
How do I push to the inner arrays in MongoDB?
[
{ emoji1: ['user19', 'user20', 'user21']},
{ emoji5: ['user12', 'user13', 'user14']},
{ emoji9: ['user29', 'user30', 'user34']}
]
I tried:
await Post
.updateOne({ _id: postID }, {
$push: {
[`reactions[0].emoji1`]: 'random user'
}
})
...where Post is my Mongoose Schema and "reactions" is the array above. I think I am doing something wrong in $push.
The result should be:
[
{ emoji1: ['user19', 'user20', 'user21', 'random user']},
{ emoji5: ['user12', 'user13', 'user14']},
{ emoji9: ['user29', 'user30', 'user34']}
]
You can use this query:
db.collection.update({
id: 1,
"reactions.emoji1": {
"$exists": true
}
},
{
"$push": {
"reactions.$.emoji1": "random user"
}
})
Example here
Got multiple documents in a db one of which looks like this:
{
"searchWord":[
"pizz",
"pizza"
],
"result":[
{
"idMeal":1,
"strMeal":"test1",
"strInstructions":"test1"
},
{
"idMeal":2,
"strMeal":"test2",
"strInstructions":"test2"
}
]
}
Tried to solve it like this:
eg:
db.meals.updateOne(
{
"searchWord": "pizz",
"result": { $elemMatch: { idMeal: "1" } }
},
{ $set: { 'result.$.strMeal' : "UPDATED" } }
)
This doesnt update the respective subdocument only the 2nd as if I wrote
{ $set: { 'result.1.strMeal' : "UPDATED" } }
(Which will result in the 2nd subdocument being updated)
This is the other idea (Same result)
db.meals.updateOne(
{ searchWord: "pizz", 'result.idMeal': 319012 },
{ $set: { "result.$.strMeal" : "fsdf" } }
)
What I dont seem to understand is its exactly the syntax that is provided by mongo yet it doesnt work
The "$" operator doesnt pick up which array of objects I wanna update
Try to use $[] in your $set for multiple positional element
db.collection.update({
"searchWord": "pizz"
},
{
$set: {
"result.$[r].strMeal": "UPDATED"
}
},
{
arrayFilters: [
{
"r.idMeal": 1
}
]
})
Here is the Mongo playground for your reference.
I want to search "search" parameter in headers, api_data, ip_address, tarih_2
where am i doing wrong? Help me please.
{
"$and":[
{
"$or":[
{
"headers":{
"$regex":".*94.54.232.96.*"
}
},
{
"api_data":{
"$regex":".*94.54.232.96.*"
}
},
{
"ip_address":{
"$regex":".*94.54.232.96.*"
}
},
{
"tarih_2":{
"$regex":".*94.54.232.96.*"
}
}
]
}
]
}
no problem with query. Thank you.
I created a nodejs app in Typescript.
I want to group documents by two fields "one_id" and "two_id" with specified one_id value.
Here is my data in my collection:
{
"_id":"5a8b2953007a1922f00124fd",
"one_id":"307973260186877954",
"two_id":"415228402765660181"
}
{
"_id":"5a8b29a3007a1922f00124fe",
"one_id":"415228402765660181",
"two_id":"307973260186877954"
}
{
"_id":"5a8c119bf6ba49302c3ef67e",
"one_id":"394199132195127306",
"two_id":"270131587092316161"
}
{
"_id":"5a8c11a4f6ba49302c3ef67f",
"one_id":"270131587092316161",
"two_id":"394199132195127306"
}
{
"_id":"5a8c33132a182308a836bc1c",
"one_id":"307973260186877954",
"two_id":"397036401075552256"
}
{
"_id":"5a8c33242a182308a836bc1d",
"one_id":"397036401075552256",
"two_id":"307973260186877954"
}
And if I want to get pairs with one_id="307973260186877954", the excepted result would be: (must have another document with "inversed" fields content)
{
"_id":"5a8b2953007a1922f00124fd",
"one_id":"307973260186877954",
"two_id":"415228402765660181"
}
{
"_id":"5a8b29a3007a1922f00124fe",
"one_id":"415228402765660181",
"two_id":"307973260186877954"
}
{
"_id":"5a8c33132a182308a836bc1c",
"one_id":"307973260186877954",
"two_id":"397036401075552256"
}
{
"_id":"5a8c33242a182308a836bc1d",
"one_id":"397036401075552256",
"two_id":"307973260186877954"
}
I don't know if you understand me.
Thank you, I hope someone will understand me!
You can use $lookup to self join the rows and output the document when there is a match and $project with exclusion to drop the joined field.
db.col.aggregate([
{"$lookup":{
"from":col,
"localField":"one_id",
"foreignField":"two_id",
"as":"onetwo"
}},
{"$lookup":{
"from":col,
"localField":"two_id",
"foreignField":"one_id",
"as":"twoone"
}},
{"$match":{"onetwo.0":{"$exists":true}, "twoone.0":{"$exists":true}}},
{"$project":{"onetwo":0,"twoone":0}}
])
I think this should be your solution. Not sure. Just gave it a try
db.TempCollections.aggregate([
{
$lookup:{
from:"TempCollections",
let:{
leftone_id:"$one_id",
lefttwo_id:"$two_id"
},
pipeline:[
{
$match: {
$expr: {
$and: [
{
$eq: [
"$one_id",
"$$lefttwo_id"
]
},
{
$eq: [
"$two_id",
"$$leftone_id"
]
}
]
}
}
}
],
as:"Final"
}
}
],
{
allowDiskUse:true
});