Use "$ in" operator with mongo-go-driver

Overview

In MongoDB, I have a $ in operator that retrieves a document that corresponds to one of several values. This time I will write what to do when you want to use $ in with mongo-go-driver.

Correspondence

As mentioned in the article How to use the elements of a slice in bson.A using mongo-go-driver 0.2.0, make the value of the array following $ in type bson.A. And set. As stated in the here documentation, bson.A is the type that handles arrays in queries.

Implementation sample

Only the setting part of the query is described as a sample. I will omit the description up to the collection acquisition, so please refer to the article I tried using the official MongoDB Go Driver by @ yotahada-nus3 for details. As the content, refill the slice that holds the content of "_id" you want to narrow down to bson.A type and set it in the query.

sample.go


func SampleGetByIDs(ids []string) error {
	//Collection acquisition process is omitted ...
	idBsonA := bson.A{}
	for _, id := range ids {
		idBsonA = append(idBsonA, id)
	}
	filter := bson.D{{Key: "_id", Value: bson.D{{Key: "$in", Value: idBsonA}}}}
	cur, err := col.Find(context.Background(), filter)
	//The cur acquisition process is omitted ...
}

Recommended Posts

Use "$ in" operator with mongo-go-driver
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Use OpenCV with Python 3 in Window
In gunicorn (> = 19.2), use max_requests_jitter along with max_requests
Use various rabbimq features with pika in python
How to use tkinter with python in pyenv
Use Python in Anaconda environment with VS Code
Use mecab-ipadic-neologd with igo-python
Use config.ini in Python
Use ansible with cygwin
Use pipdeptree with virtualenv
[Python] Use JSON with Python
Use Mock with pytest
Use DataFrame in Java
Use dates in Python
Use indicator with pd.merge
Use Mean in DataFrame
Use Gentelella with django
Use Valgrind in Python
Use mecab with Python3
Use tensorboard with Chainer
Use DynamoDB with Python
Use pip with MSYS2
Use Python 3.8 with Anaconda
Use pyright with Spacemacs
Use ujson in requests
Use python with docker
Use TypeScript with django-compressor
Use LESS with Django
Use profiler in Python
Use MySQL with Django
Use Enums with SQLAlchemy
Use tensorboard with NNabla
Use GPS with Edison
Use nim with Jupyter
Use Cursur that closes automatically with sqlite3 in Python
Let's use def in python
Use Trello API with python
Use shared memory with shared libraries
Use custom tags with PyYAML
Use directional graphs with networkx
Use TensorFlow with Intellij IDEA
Use let expression in Python
Use Anaconda in pyenv environment
Scraping with selenium in Python
Use Measurement Protocol in Python
Working with LibreOffice in Python
Scraping with chromedriver in python
Use callback function in Python
Use Twitter API with Python
Use parameter store in Python
Use pip with Jupyter Notebook
Debugging with pdb in Python
You cannot use break or continue with the ternary operator
Use HTTP cache in Python
Use DATE_FORMAT with SQLAlchemy filter
Specify a date range with a comparison operator in Python's datetime
Use TUN / TAP with Python
Use sqlite3 with NAO (Pepper)
Use regular expressions in C