Dynamically create Aggregate pipeline for mongo-go-driver
https://godoc.org/github.com/mongodb/mongo-go-driver
I am trying to create an Aggregate pipeline dynamically. For example, I want to read a slice of string containing the oceans. I tried breaking these apart to pieces, but I could not find any methods to append elements.
pipeline := bson.NewArray(
bson.VC.DocumentFromElements(
bson.EC.SubDocumentFromElements(
"$match",
bson.EC.SubDocumentFromElements("ocean",
bson.EC.ArrayFromElements("$in",
bson.VC.String("Pacific Ocean"),
//bson.VC.String("Indian Ocean"),
),
),
bson.EC.SubDocumentFromElements("callTypeName",
bson.EC.ArrayFromElements("$in",
bson.VC.String("Wookie"),
bson.VC.String("Unknown 13"),
),
),
),
),
)
cur, err := collection.Aggregate(context.Background(), pipeline)
mongodb go
add a comment |
https://godoc.org/github.com/mongodb/mongo-go-driver
I am trying to create an Aggregate pipeline dynamically. For example, I want to read a slice of string containing the oceans. I tried breaking these apart to pieces, but I could not find any methods to append elements.
pipeline := bson.NewArray(
bson.VC.DocumentFromElements(
bson.EC.SubDocumentFromElements(
"$match",
bson.EC.SubDocumentFromElements("ocean",
bson.EC.ArrayFromElements("$in",
bson.VC.String("Pacific Ocean"),
//bson.VC.String("Indian Ocean"),
),
),
bson.EC.SubDocumentFromElements("callTypeName",
bson.EC.ArrayFromElements("$in",
bson.VC.String("Wookie"),
bson.VC.String("Unknown 13"),
),
),
),
),
)
cur, err := collection.Aggregate(context.Background(), pipeline)
mongodb go
You mean likeAppend()
? It's a little broad a question since you don't actually say "which parts" you want to "dynamically" construct. So I'm a little unsure how to interpret your question other than you appear to not be aware of where the API documentation is. As you can see by visiting the links, there's quite a bit of it. Perhaps you coul be more descriptive in your question about what "dynamic construction" you actually intend to do here.
– Neil Lunn
Nov 13 '18 at 0:12
add a comment |
https://godoc.org/github.com/mongodb/mongo-go-driver
I am trying to create an Aggregate pipeline dynamically. For example, I want to read a slice of string containing the oceans. I tried breaking these apart to pieces, but I could not find any methods to append elements.
pipeline := bson.NewArray(
bson.VC.DocumentFromElements(
bson.EC.SubDocumentFromElements(
"$match",
bson.EC.SubDocumentFromElements("ocean",
bson.EC.ArrayFromElements("$in",
bson.VC.String("Pacific Ocean"),
//bson.VC.String("Indian Ocean"),
),
),
bson.EC.SubDocumentFromElements("callTypeName",
bson.EC.ArrayFromElements("$in",
bson.VC.String("Wookie"),
bson.VC.String("Unknown 13"),
),
),
),
),
)
cur, err := collection.Aggregate(context.Background(), pipeline)
mongodb go
https://godoc.org/github.com/mongodb/mongo-go-driver
I am trying to create an Aggregate pipeline dynamically. For example, I want to read a slice of string containing the oceans. I tried breaking these apart to pieces, but I could not find any methods to append elements.
pipeline := bson.NewArray(
bson.VC.DocumentFromElements(
bson.EC.SubDocumentFromElements(
"$match",
bson.EC.SubDocumentFromElements("ocean",
bson.EC.ArrayFromElements("$in",
bson.VC.String("Pacific Ocean"),
//bson.VC.String("Indian Ocean"),
),
),
bson.EC.SubDocumentFromElements("callTypeName",
bson.EC.ArrayFromElements("$in",
bson.VC.String("Wookie"),
bson.VC.String("Unknown 13"),
),
),
),
),
)
cur, err := collection.Aggregate(context.Background(), pipeline)
mongodb go
mongodb go
asked Nov 12 '18 at 23:56
carsomyrxp
31
31
You mean likeAppend()
? It's a little broad a question since you don't actually say "which parts" you want to "dynamically" construct. So I'm a little unsure how to interpret your question other than you appear to not be aware of where the API documentation is. As you can see by visiting the links, there's quite a bit of it. Perhaps you coul be more descriptive in your question about what "dynamic construction" you actually intend to do here.
– Neil Lunn
Nov 13 '18 at 0:12
add a comment |
You mean likeAppend()
? It's a little broad a question since you don't actually say "which parts" you want to "dynamically" construct. So I'm a little unsure how to interpret your question other than you appear to not be aware of where the API documentation is. As you can see by visiting the links, there's quite a bit of it. Perhaps you coul be more descriptive in your question about what "dynamic construction" you actually intend to do here.
– Neil Lunn
Nov 13 '18 at 0:12
You mean like
Append()
? It's a little broad a question since you don't actually say "which parts" you want to "dynamically" construct. So I'm a little unsure how to interpret your question other than you appear to not be aware of where the API documentation is. As you can see by visiting the links, there's quite a bit of it. Perhaps you coul be more descriptive in your question about what "dynamic construction" you actually intend to do here.– Neil Lunn
Nov 13 '18 at 0:12
You mean like
Append()
? It's a little broad a question since you don't actually say "which parts" you want to "dynamically" construct. So I'm a little unsure how to interpret your question other than you appear to not be aware of where the API documentation is. As you can see by visiting the links, there's quite a bit of it. Perhaps you coul be more descriptive in your question about what "dynamic construction" you actually intend to do here.– Neil Lunn
Nov 13 '18 at 0:12
add a comment |
1 Answer
1
active
oldest
votes
I thought the question was pretty clear, not sure if the first commentor was actually reading the statement carefully.
What this person was asking was to dynamically insert data given a list of data into the pipeline.
I had the same issue on a vue app my team and I are working on. Using your provided data, here is the general template:
Given a slice of string of oceans
a := string{"Pacific Ocean", "Indian Ocean"}
Make a slice of size 0 of type *bson.Value
b := make(*bson.Value, 0)
Loop through the slice of oceans and append bson converted values to slice b
for _, v := range a {
b = append(b, bson.VC.String(v))
}
Then create key-value pair so that mongo can look for matches
c := bson.EC.ArrayFromElements("$in", b...)
Then pass c into the pipeline
pipeline := bson.NewArray(
bson.VC.DocumentFromElements(
bson.EC.SubDocumentFromElements(
"$match",
bson.EC.SubDocumentFromElements("ocean", c),
),
),
)
This should give you an idea on how to dynamically pipeline for callTypeNames
This works for me. I totally forgot about passing a slice on a variadic function. Thank you!
– carsomyrxp
Nov 13 '18 at 1:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271818%2fdynamically-create-aggregate-pipeline-for-mongo-go-driver%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I thought the question was pretty clear, not sure if the first commentor was actually reading the statement carefully.
What this person was asking was to dynamically insert data given a list of data into the pipeline.
I had the same issue on a vue app my team and I are working on. Using your provided data, here is the general template:
Given a slice of string of oceans
a := string{"Pacific Ocean", "Indian Ocean"}
Make a slice of size 0 of type *bson.Value
b := make(*bson.Value, 0)
Loop through the slice of oceans and append bson converted values to slice b
for _, v := range a {
b = append(b, bson.VC.String(v))
}
Then create key-value pair so that mongo can look for matches
c := bson.EC.ArrayFromElements("$in", b...)
Then pass c into the pipeline
pipeline := bson.NewArray(
bson.VC.DocumentFromElements(
bson.EC.SubDocumentFromElements(
"$match",
bson.EC.SubDocumentFromElements("ocean", c),
),
),
)
This should give you an idea on how to dynamically pipeline for callTypeNames
This works for me. I totally forgot about passing a slice on a variadic function. Thank you!
– carsomyrxp
Nov 13 '18 at 1:50
add a comment |
I thought the question was pretty clear, not sure if the first commentor was actually reading the statement carefully.
What this person was asking was to dynamically insert data given a list of data into the pipeline.
I had the same issue on a vue app my team and I are working on. Using your provided data, here is the general template:
Given a slice of string of oceans
a := string{"Pacific Ocean", "Indian Ocean"}
Make a slice of size 0 of type *bson.Value
b := make(*bson.Value, 0)
Loop through the slice of oceans and append bson converted values to slice b
for _, v := range a {
b = append(b, bson.VC.String(v))
}
Then create key-value pair so that mongo can look for matches
c := bson.EC.ArrayFromElements("$in", b...)
Then pass c into the pipeline
pipeline := bson.NewArray(
bson.VC.DocumentFromElements(
bson.EC.SubDocumentFromElements(
"$match",
bson.EC.SubDocumentFromElements("ocean", c),
),
),
)
This should give you an idea on how to dynamically pipeline for callTypeNames
This works for me. I totally forgot about passing a slice on a variadic function. Thank you!
– carsomyrxp
Nov 13 '18 at 1:50
add a comment |
I thought the question was pretty clear, not sure if the first commentor was actually reading the statement carefully.
What this person was asking was to dynamically insert data given a list of data into the pipeline.
I had the same issue on a vue app my team and I are working on. Using your provided data, here is the general template:
Given a slice of string of oceans
a := string{"Pacific Ocean", "Indian Ocean"}
Make a slice of size 0 of type *bson.Value
b := make(*bson.Value, 0)
Loop through the slice of oceans and append bson converted values to slice b
for _, v := range a {
b = append(b, bson.VC.String(v))
}
Then create key-value pair so that mongo can look for matches
c := bson.EC.ArrayFromElements("$in", b...)
Then pass c into the pipeline
pipeline := bson.NewArray(
bson.VC.DocumentFromElements(
bson.EC.SubDocumentFromElements(
"$match",
bson.EC.SubDocumentFromElements("ocean", c),
),
),
)
This should give you an idea on how to dynamically pipeline for callTypeNames
I thought the question was pretty clear, not sure if the first commentor was actually reading the statement carefully.
What this person was asking was to dynamically insert data given a list of data into the pipeline.
I had the same issue on a vue app my team and I are working on. Using your provided data, here is the general template:
Given a slice of string of oceans
a := string{"Pacific Ocean", "Indian Ocean"}
Make a slice of size 0 of type *bson.Value
b := make(*bson.Value, 0)
Loop through the slice of oceans and append bson converted values to slice b
for _, v := range a {
b = append(b, bson.VC.String(v))
}
Then create key-value pair so that mongo can look for matches
c := bson.EC.ArrayFromElements("$in", b...)
Then pass c into the pipeline
pipeline := bson.NewArray(
bson.VC.DocumentFromElements(
bson.EC.SubDocumentFromElements(
"$match",
bson.EC.SubDocumentFromElements("ocean", c),
),
),
)
This should give you an idea on how to dynamically pipeline for callTypeNames
answered Nov 13 '18 at 1:10
Leesa
627
627
This works for me. I totally forgot about passing a slice on a variadic function. Thank you!
– carsomyrxp
Nov 13 '18 at 1:50
add a comment |
This works for me. I totally forgot about passing a slice on a variadic function. Thank you!
– carsomyrxp
Nov 13 '18 at 1:50
This works for me. I totally forgot about passing a slice on a variadic function. Thank you!
– carsomyrxp
Nov 13 '18 at 1:50
This works for me. I totally forgot about passing a slice on a variadic function. Thank you!
– carsomyrxp
Nov 13 '18 at 1:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271818%2fdynamically-create-aggregate-pipeline-for-mongo-go-driver%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You mean like
Append()
? It's a little broad a question since you don't actually say "which parts" you want to "dynamically" construct. So I'm a little unsure how to interpret your question other than you appear to not be aware of where the API documentation is. As you can see by visiting the links, there's quite a bit of it. Perhaps you coul be more descriptive in your question about what "dynamic construction" you actually intend to do here.– Neil Lunn
Nov 13 '18 at 0:12