Removing a document from array of documents in mongodb using c# - arrays

I want to remove the highest value price document
{
"name" : "Ryan",
"price" : 6.5
}
from
db.books.find({_id:5}).pretty()
{
"_id" : 5,
"name" : "Big Book",
"authors" : [
{
"name" : "Ryan",
"price" : 5.5
},
{
"name" : "Ryan",
"price" : 6.5
}
]
}
using c# driver
I have the removable document in c# front end in a collection.
But my following update statement is not removing the document.
var filter = new BsonDocument("_id", x._id);
var docToRemove = new BsonDocument("name", x.name);docToRemove.Add("price", x.price);
var update = Builders<Book>.Update.Pull("books.authors", docToRemove);
var result = await col.FindOneAndUpdateAsync(filter, update);

Do you have required value in x.price?
I didn't test it for exactly your case, but try this:
var update = Builders<Book>.Update.PullFilter(p => p.authors,
f => f.price == x.price);
var result = await collection.FindOneAndUpdateAsync(p => p._id == 5, update);

Related

Rename a sub-document field within an Array of an Document

I am trying to rename the particular child field inside an array of an collection in MONGODB
{
"_id" : Id("248NSAJKH258"),
"isGoogled" : true,
"toCrawled" : true,
"result" : [
{
"resultsId" : 1,
"title" : "Text Data to be writen",
"googleRanking" : 1,
"isrelated" : false
},
{
"resultId" : 2,
"title" : "Text Data",
"googleRanking" : 2,
"isrelated" : true
}]
**I need to rename "isrelated" to "related" ** from the collection document
Using mongo Version 4.0
I Tried :
db.collection_name.update({}, { $rename: { 'result.isrelated': 'result.related'} } )
But it didn't worked in my case
As mentioned in official documentation $rename not working for arrays.
Please check link below:
https://docs.mongodb.com/manual/reference/operator/update/rename/
But you can do something like this
let newResult = [];
db.aa1.find({}).forEach(doc => {
for (let i in doc.result) {
newResult.push({
"resultsId" : doc.result[i]['resultsId'],
"title" : doc.result[i]['title'],
"googleRanking" : doc.result[i]['googleRanking'],
"related" : doc.result[i]['isrelated'],
})
}
db.aa1.updateOne({_id: doc._id}, {$set: {result: newResult}});
newResult = []
})

es6 Loop through object array and aggregate unique values from key pair (cleanest way)

What's the cleanest and es6 native (if possible) way to loop through a object array to grab each unique value. Example would like this :
[{
"name" : "joe",
},
,{
"name" : "jean",
},
{
"name" : "joe",
},
{
"name" : "joe",
},
{
"name" : "mike",
}]
and in my results I want to see only : joe, jean, mike (only unique values, no dupes)
Since you mentioned ES6, it seems like a Set object would be what you want since it will do the uniqueness part for you and should do so fairly efficiently:
var objs = [{"name" : "joe"},{"name" : "jean"},{"name" : "joe"},{"name" : "joe"},{"name" : "mike"}];
let uniqueNames = Array.from(new Set(objs.map(item => item.name)));
console.log(uniqueNames);
Run this snippet to see the results.
a = [{name:"joe"},{name:"jean"},{name:"joe"},{name:"joe"},{name:"mike"}]
console.log(_.uniq(_.map(a, 'name'))) // Lodash 0.1.0
console.log([...new Set(a.map(o => o.name))]) // ES6
<script src="https://cdn.jsdelivr.net/npm/lodash#4.17.4/lodash.min.js"></script>
var objs = [{"name" : "joe"},{"name" : "jean"},{"name" : "joe"},{"name" : "joe"},{"name" : "mike"}];
var uniqueNames = objs.map( obj => obj.name )
.filter( (name, idx, arr) => { return arr.indexOf(name) === idx; } );
The .map extracts an array of the name values, and the .filter returns only the unique elements (first instances only).

Mongoose update array property

I am trying to update a document via Mongoose, but it does not update the array property.
Here's an example document:
{
"_id" : "55da477a9bfc910e38zzccf2",
"projectname" : "ASong",
"owner" : "adam",
"tracks" : [{
"name" : "Bass",
"file" : "upload/Bass.mp3",
"volume" : "0.75",
"pan" : "0.65"
}, {
"file" : "upload/Drums.mp3",
"volume" : "0.4",
"pan" : "-0.75",
"name" : "Drums"
}
],
"users" : ["adam", "eve"]
}
if I pass to Mongoose an object and try to use it to update like this:
var id = req.body._id;
var editedProject = req.body;
delete editedProject._id;
console.log("TRACKS: " +JSON.stringify(editedProject.tracks));
Project.update({"_id": id}, editedProject, {"upsert": true}, function(err, proj) {
if (err) {
res.send(err);
}
res.json(proj);
});
Only the 1st level properties are updated, but not the array.
I've found some solutions that involve looping through the array elements and calling an update for each element, but I would like to avoid that and keep the update as a single operation.
Or is it better to avoid using arrays?

How to retrieve data from an array [Mongodb]

I am trying to extract data from an array in a collection, the extract of code is shown below:
> db.nodes.findOne()
{
"_id" : NumberLong(24060429),
"_t" : "OsmNode",
"uname" : "studerap",
"uid" : 7260,
"version" : 2,
"changeset" : 634057,
"timestamp" : ISODate("2007-11-27T11:18:58Z"),
"tags" : [
[
"created_by",
"almien_coastlines"
],
[
"source",
"PGS"
]
],
"tagKeys" : [
"created_by",
"source"
],
"location" : [
5.5442938804626465,
-6.488432884216309
]
}
The data i actually want to retrieve is 5.5442938804626465 from the location array. Shall it be retrieved through index?
Thanks for helping
To extract that indexed element of array from all documents, you can get through:
var index = 0, count = 1;
var cursor = db.nodes.find({}, {_id:1, location:{$slice:[index, count]}});
Of course, you can write directly:
var cursor = db.nodes.find({}, {_id:1, location:{$slice:[0, 1]}});
Or
var cursor = db.nodes.find({}, {_id:1, location:{$slice:1}});
Then, extract the result:
var results = [];
cursor.forEach(function(e) {
results.push(e.location[0]);
});
By the way, .find() returns a cursor; and .findOne() returns a document, which is equivalent to running .find().next() once if cursor has documents.

Mongodb: Query a json-object nested in an array

I'm quite new to mongodb and there is one thing I can't solve right now:
Let's pretend, you have the following document (simplified):
{
'someKey': 'someValue',
'array' : [
{'name' : 'test1',
'value': 'value1'
},
{'name' : 'test2',
'value': 'value2'
}
]
}
Which query would return the json-object, in which the value equals 'value2'?
That means, i need this json-object:
{
'name' : 'test2',
'value': 'value2'
}
Of course I already tried a lot of possible queries, but none of them returned the right, e.g.
db.test.find({'array.value':'value2'})
db.test.find({'array.value':'value2'}, {'array.value':1})
db.test.find({'array.value':'value2'}, {'array.value':'value2'})
Can someone help and show me, what I'm doing wrong?
Thanks!
Using Positional operator
db.test.find(
{ "array.value": "value2" },
{ "array.$": 1, _id : 0 }
)
Output
{ "array" : [ { "name" : "test2", "value" : "value2" } ] }
Using aggregation
db.test.aggregate([
{ $unwind : "$array"},
{ $match : {"array.value" : "value2"}},
{ $project : { _id : 0, array : 1}}
])
output
{ "array" : { "name" : "test2", "value" : "value2" } }
Using Java Driver
MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017));
DB db = mongoClient.getDB("mydb");
DBCollection collection = db.getCollection("test");
DBObject unwind = new BasicDBObject("$unwind", "$array");
DBObject match = new BasicDBObject("$match", new BasicDBObject(
"array.value", "value2"));
DBObject project = new BasicDBObject("$project", new BasicDBObject(
"_id", 0).append("array", 1));
List<DBObject> pipeline = Arrays.asList(unwind, match, project);
AggregationOutput output = collection.aggregate(pipeline);
Iterable<DBObject> results = output.results();
for (DBObject result : results) {
System.out.println(result.get("array"));
}
output
{ "name" : "test2" , "value" : "value2"}
Try the $in operator like this:
db.test.find({"array.value" : { $in : ["value2"]}})
You can pass multiple find objects in element match for getting the exact answer.
db.test.find({'array':{$elemMatch:{value:"value2"}})
output: {'name' : 'test1','value': 'value1'}
Use $elemMatch and dot(.) to get your required output
db.getCollection('mobiledashboards').find({"_id": ObjectId("58c7da2adaa8d031ea699fff") },{ viewData: { $elemMatch : { "widgetData.widget.title" : "England" }}})

Resources