CRUD Java Flashcards

1
Q

True or False

BSON objects are more secure than JSON objects

A

True. BSON objects are not vulnerable to JSON injection attacks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which class does MongoDB recommend to represent BSON documents?

A

The Document class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is another name for “subdocument”?

A

Embedded document
Nested document

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Code an example of inserting a single document

A
collection.insertOne(new Document(field, value));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Code an example of inserting multiple documents

A
var doc1 = new Document().append(field, value);
var doc2 = new Document().append(field, value);
List<Document> docs = Arrays.asList(doc1, doc2);
// List.of(...) also works
collection.insertMany(docs);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which factory class should you use for query documents?

A

The Filters class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Code an example of finding one document in the database

A
collection.find(eq(field, value)).first();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Code an example of obtaining and iterating through a cursor

A
try (var cursor = collection.find(eq(field, value)).cursor()) {
    while (cursor.hasNext()) {
        //...
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

True or False

An application should ensure that a cursor is closed in all circumstances, e.g. using a try-with-resources statement

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which factory class should you use for update operators?

A

The Updates class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Code an example of updating multiple fields in a single document

A
collection.updateOne(query, combine(set(field, value), set(field2, value2)));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Code an example of inserting 2 documents to 2 different collections as a transaction

A
MongoClient client = MongoClients.create("...");
MongoCollection<Document> collection1 = client.getDatabase(name1).getCollection(name1);
MongoCollection<Document> collection2 = client.getDatabase(name2).getCollection(name2);

TransactionBody transaction = new TransactinBody<Void>() {
    public void execute() {
		    collection1.insertOne(clientSession, new Document(field, value));
				collection2.insertOne(clientSession, new Document(field, value));
}

try (ClientSession clientSession = client.startSession()) {
    clientSession.withTransaction(transaction);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

True or False

Transactions can only be used for write operations

A

False. Transactions can be used for both read and write operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

True or False

Transactions are ACID compliant

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

True or False

By default, MongoDB will cancel any transaction that runs for more than 80 seconds

A

False. Its 60 seconds

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Code an example of replacing a document and insert set to true

A
collection.replaceOne(eq(field, value), new Document(field, value), new ReplaceOptions().upsert(true));
17
Q

Code an example counting documents

A
collection.countDocuments(eq(field, value))
18
Q

Code an example limiting the number of documents found

A
collection.find().limit(number);
19
Q

Code an example of sorting documents in ascending order on multiple fields for a simple query

A
collection.find().sort(Sorts.ascending(field1, field2, ...))
20
Q

Code an example projecting multiple fields (include) and excluding the _id field for a simple query

A
collection.find().projection(Projections.fields(Projections.excludeId(), Projections.include(field1, field2)));
21
Q

Code an example updating a document with upsert set to true

A
collection.updateOne(Filters..., Updates.set(field, value), new UpdateOptions().upsert(true))