DB2 Nosql / MongoDB 'An operator that starts with $ is expected for projectid' error? - db2

I have a list of documents with the structure:
{
from:"string"
to:"string"
payload:{
projectid:10000
}
}
I want to delete all documents with payload.projectid set to 10000 using the java API.
From the command-line I write:
db.notifications.find({"payload":{"projectid":10000}})
Error:
java.lang.RuntimeException: An operator that starts with $ is expected for projectid.
A similar error arises if I delete, whether through cli or programmatically.
This works in normal mongoDB.
How do I delete in db2 nosql?
Java code (for your reference!):
BasicDBObject query = new BasicDBObject();
query.append("to", "username");
query.append("from", "username2");
query.append("payload", new BasicDBObject().append("projectid", 10000));
System.out.println(query); // prints out okay
col.remove(query); //error

You should be using dot notation to reference the sub-document field:
Link
In your case it would be:
db.notifications.find({"payload.projectid":10000})
In Java the find should look something like this:
DBCollection notifications = db.getCollection("notifications")
DBObject subdocumentQuery = new BasicDBObject("payload.projectid", 10000);
DBCursor subdocumentCursor = notifications.find(subdocumentQuery);

Related

Mongodb $set and $inc in camel

I am attempting to use the $set and $inc flags to update a field and increment a field by 1 in camel ( java ). Here is the code I am using:
from("direct:mySampleRoute").routeId("update-mongo-db-entry").setBody()
.simple("{\"person\": \"${headers.personName}\", \"job\": \"computerGuy\",\"title\": \"DR\"},"
+ "{\"$set\": {\"pay\": \"${headers.newPay}\"}, \"$inc\": {\"hrPay\": 1}}")
.toD("mongodb:mongoClient?database=" + mongoDb + "&collection="
+ mongoEmplyeeCollection + "&operation=update")
.end();
The goal of the query is to find an entry where person == ${headers.personName}, job == computerGuy, and title = DR. Once it is found, it is to set the pay field to ${headers.newPay} and inc the hrPay field by positive 1. From what I can tell, I am structuring my code exactly the same as mentioned in this post: What am I doing wrong with $set and $inc in update However, once executed, my app crashes and I am unable to see any logs wrt to why that query fails. As such, I suspect I have a fundamental problem in structuring the json update. Camel documentation located here is of limited help.
Using camel, how can I achieve my goal of making a "select" query, updating some fields using $set, and incrementing a field using $inc ?
I ended up solving my problem using a Processor to create the needed body. I slved it using this syntext within the process method:
#Override
public void process(final Exchange exchange) throws Exception {
final BasicDBObject filterQuery = new BasicDBObject();
filterQuery.append("person", exchange.getIn().getHeader("personName"));
filterQuery.append("job", "computerGuy");
filterQuery.append("title", "DR");
final BasicDBObject updateQuery = new BasicDBObject();
updateQuery.append("$set", new BasicDBObject("pay", exchange.getIn().getHeader("newPay"))
.append("location", "4th floor"));
updateQuery.append("$inc", new BasicDBObject("hrPay", 1));
final List<DBObject> query = new ArrayList<>();
query.add(filterQuery);
query.add(updateQuery);
exchange.getIn().setBody(query);
}

Checking if an Index exists in mongodb

Is there a command that i can use via javascript in mongo shell that can be used to check if the particular index exists in my mongodb. I am building a script file that would create indexes. I would like that if I run this file multiple number of times then the indexes that already exists are not recreated.
I can use db.collection.getIndexes() to get the collection of all the indexes in my db and then build a logic to ignore the ones that already exists but i was wondering if there is command to get an index and then ignore a script that creates the index. Something like:
If !exists(db.collection.exists("indexname"))
{
create db.collectionName.CreateIndex("IndexName")
}
Creating indexes in MongoDB is an idempotent operation. So running db.names.createIndex({name:1}) would create the index only if it didn't already exist.
The deprecated (as of MongoDB 3.0) alias for createIndex() is ensureIndex() which is a bit clearer on what createIndex() actually does.
Edit:
Thanks to ZitRo for clarifying in comments that calling createIndex() with the same name but different options than an existing index will throw an error MongoError: Index with name: **indexName** already exists with different options as explained in this question.
If you have other reasons for checking, then you can access current index data one of two ways:
As of v3.0, we can use db.names.getIndexes() where names is the name of the collection. Docs here.
Before v3.0, you can access the system.indexes collection and do a find as bri describes below.
Use db.system.indexes and search on it.
If, for example, you have an index called 'indexname', you can search for it like this:
db.system.indexes.find({'name':'indexname'});
If you need to search for that index on a specific collection,then you need to use the ns property (and, it would be helpful to have the db name).
db.system.indexes.find({'name':'indexname', 'ns':'dbname.collection'});
Or, if you absolutely hate including the db name...
db.system.indexes.find({'name':'indexname', 'ns': {$regex:'.collection$'}});
Pulling that together...
So, you're finished check would be:
if(db.system.indexes.find({name:'indexname',ns:{$regex:'.collection$'}}).count()==0) {
db.collection.createIndex({blah:1},{name:'indexname'})
}
Using nodeJS MongoDB driver version 2.2:
const MongoClient = require('mongodb').MongoClient;
exports.dropOldIndexIfExist = dropOldIndexIfExist;
async function dropOldIndexIfExist() {
try {
const mongoConnection = MongoClient.connect('mongodb://localhost:27017/test');
const indexName = 'name_1';
const isIndexExist = await mongoConnection.indexExists(indexName);
if (isIndexExist === true) {
await mongoConnection.dropIndex(indexName);
}
} catch (err) {
console.error('dropOldIndexIfExist', err.message);
throw err;
}
}
I've created a custom method in c# to check if the index exists, using mongo driver:
public bool IndexExists<TDocument>(
IMongoCollection<TDocument> collection, string name)
{
var indexes = collection.Indexes.List().ToList();
var indexNames = indexes
.SelectMany(index => index.Elements)
.Where(element => element.Name == "name")
.Select(name => name.Value.ToString());
return indexNames.Contains(name);
}
maybe we can use something like https://docs.mongodb.com/v3.2/reference/method/db.collection.getIndexes/#db.collection.getIndexes to check if the collection have an index equal to something ?
if yes then drop and add the new one or add the new one directly
In my case i did as follows.
DBCollection yourcollectionName = mt.getCollection("your_collection");
if (yourcollectionName.getIndexInfo() == null || yourcollectionName.getIndexInfo().isEmpty()) {
DBObject indexOptions = new BasicDBObject();
indexOptions.put("pro1", 1);
indexOptions.put("pro2", 1);
yourcollectionName.createIndex(indexOptions, "name_of_your_index", true);
}
Here is a Python 3.5+ and pymongo >= 4.1 (type hints) function that I wrote which checks to see if the index name exists (other details about the index are omitted).
from pymongo import MongoClient
from pymongo.collection import Collection
def check_collection_indexes(db: MongoClient, collection_name: str, index_name: str) -> bool:
coll: Collection = db[collection_name]
indexes: dict = coll.index_information()
# assume false
found = False
# get length of the index name for substring
l = len(index_name)
for k in indexes.keys():
# Substring the keys and check for match
if k[:l] == index_name:
found = True
break
else:
found = False
return found
If the index exists it will return True, otherwise you can use the False output to call another function that creates/recreates the indexes.

Index hint with mongodb csharp

I am migrating from the mongodb csharp driver 1.10.0 to 2.0.0.
One of the collection I am using is very big and has to fulfill many queries with different filter attributes. That is why I was relying on some index hint statements. With the v1.10 driver it looks like
myCollection.Find(query).SetHint("myIndexName");
I searched the v2 driver api but this hint method seems to be completly removed in the v2 driver. Is there an alternative? How should I do index hints with the v2 driver?
Note: The Solutions provided works for latest mongodb csharp drivers as well
You can use the FindOptions.Modifiers property.
var modifiers = new BsonDocument("$hint", "myIndexName");
await myCollection.Find(query, new FindOptions { Modifiers = modifiers }).ToListAsync();
May I ask why you are using the hint? Was the server consistently choosing the wrong index? You shouldn't need to do this except in exceptional cases.
Craig
Ideally, try to make the query in a way that mongodb optimizer can use the index automatically.
If you are using FindAsync then you will have a property named Hint. Use it like this:
If you have index named "myIndexName" which you want your query should use forcefully, then use like this:.
BsonString bsonString = new BsonString("myIndexName");
cursor = await collection.FindAsync(y => y.Population > 400000000,
new FindOptions<Person, Person>()
{
BatchSize = 200,
NoCursorTimeout = true,
AllowPartialResults = true,
Projection = "{'_id':1,'Name':1,'Population':1}"
Hint = bsonString.AsBsonValue,
}).ConfigureAwait(false);
You can fine BsonString class in MongoDB.Bson
With agregate you can force indice like this:
BsonString bsonString = new BsonString("ix_indice");
var query = this.collection.Aggregate(new AggregateOptions() { Hint = bsonString }).Match(new BsonDocument {..});
If you are using the Linq IQueryable, you can specify the hint (and other options) like this:
BsonDocument hint = new BsonDocument("myFieldName", 1);
// or
BsonDocument hint = new BsonString("myIndexName");
await collection.AsQueryable(new AggregateOptions { Hint = hint })
myFieldName can also reference a complex field, e.g. Metadata.FileName
myIndexName is the name of an index. I prefer to reference the field (first option) directly, instead of an index, for simple cases.

How to use Mongo Bulk Update using its Java Driver?

I am using Mongo Bulk Update using its Java Driver 2.13.
MongoClient mongo = new MongoClient("localhost", 27017);
DB db = (DB) mongo.getDB("test");
DBCollection collection = db.getCollection("collection");
BulkWriteOperation builder = collection.initializeOrderedBulkOperation();
builder.find(new BasicDBObject("_id", "1")).update(new BasicDBObject("_id", "1").append("name", "dev"));
I got the following exception:
Caused by: java.lang.IllegalArgumentException: Update document keys must start with $: _id
at com.mongodb.DBCollectionImpl$Run.executeUpdates(DBCollectionImpl.java:769)
at com.mongodb.DBCollectionImpl$Run.execute(DBCollectionImpl.java:734)
at com.mongodb.DBCollectionImpl.executeBulkWriteOperation(DBCollectionImpl.java:149)
at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:1737)
at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:1733)
at com.mongodb.BulkWriteOperation.execute(BulkWriteOperation.java:93)
The reason for the error is because when doing updates you must use an update operator. See http://docs.mongodb.org/manual/reference/operator/update/ for a list of update operators.
From your example I gather you are setting the name field to "dev", so you need to use the $set operator like so:
builder.find(new BasicDBObject("_id", "1"))
.update(new BasicDBObject("$set", new BasicDBObject("name", "dev")));

Indexing MongoDB collection in Java

I am creating a collection in MongoDB in the following way and I want to create a 2dsphere index on location field of this collection from Java code. But I am not able to do so.
collection.ensureIndex() method expects a DBObject as a parameter but I cannot pass location to it.
How do I create collection.ensureIndex({"location" : "2dsphere"}) in Java code?
MongoDB allows me to do so in command prompt. But, I want to index it through code written in Java.
BasicDBObject doc = new BasicDBObject("attr1", nextLine[0])
.append("attr2", nextLine[1])
.append("edge-metro-code", nextLine[6])
.append("location", new BasicDBObject("type", "Point")
.append("coordinates",latLong))
.append("attr3", nextLine[9])
.append("attr4", nextLine[10])
ensureIndex() has been deprecated now. You should use createIndex() instead:
MongoClient mongoClient = new MongoClient();
DBCollection test = mongoClient.getDB("testdb").getCollection("test");
test.createIndex(new BasicDBObject("location","2dsphere"));
You should construct a new DBObject that represents your index. See the code bellow:
DBObject index2d = BasicDBObjectBuilder.start("location", "2dsphere").get();
DBCollection collection = new Mongo().getDB("yourdb").getCollection("yourcollection");
collection.ensureIndex(index2d);