MongoDB: Query Builder to Update Field - arrays

I'm trying update a field in my mongodb collection with c# driver.
:: The Person Object
{
Name: 'Person1'
Prefers: [
{ Name: 'Prefer1', Location: 'Lisbon',
Sports: [ { Name: 'Sports1', Indoor: true, OutDoor: false } ]
]
}
My 'Sports' objects is a List of interface: ISports
How I update the field Person.Prefers.Sports.Name?
The next code don't work for me:
var filter = Builders<Person>.Filter.And(
Builders<Person>.Filter.Eq(i => i.Id, X),
Builders<Person>.Filter.ElemMatch(i => i.Prefers, i => i.Id == Y)
Builders<Person>.Filter.Eq("Person.Prefers.Sports.Id", Z)
);
var update = Builders<Person>.Update.Set("Prefers.Sports.$.Name", "Sports2");
UpdateOne(filter , update);
Thanks

Related

sort an array of objects on multiple key using array-sort

I have a requirement where the array of objects needs to be sorted on certain keys. The keys with which it needs to be sorted is dynamic and it is Not fixed.
I came across array-sort in npm library. Using that, am able to sort on multiple keys but it sorts only on ascending order.
const input = [{id:'1',name:'John',city:'Denver',State:'CO'},
{id:'2',name:'Smith',city:'San Fransisco',State:'CA'},
{id:'3',name:'Adam',city:'Concord',State:'CA'},
{id:'1',name:'John',city:'Concord',State:'CA'}]
I want to sort on State (asc), city (asc) and id (desc). My output should look like
[
{id:'3',name:'Adam',city:'Concord',State:'CA'},
{id:'1',name:'John',city:'Concord',State:'CA'},
{id:'2',name:'Smith',city:'San Fransisco',State:'CA'},
{id:'1',name:'John',city:'Denver',State:'CO'}]
Can anyone please let me know how i can implement sorting on descending using array-sort
Thanks
Maybe you want a JavaScript function like this?
function multicolumnSort(data, orders) {
return data.sort((e1, e2) => {
for (var i = 0; i < orders.length; i++)
if (e1[orders[i].column] != e2[orders[i].column])
return orders[i].desc ^ e2[orders[i].column] < e1[orders[i].column]? 1 : -1;
return 0;
});
}
Then, you may call the function with your order keys:
let orders = [
{
column: 'State'
},
{
column: 'city'
},
{
column: 'id',
desc: true
}
];
let result = multicolumnSort(input, orders);
Check my code
function DESC(i, ii) { // DESC
return (i[key] > ii[key]) ? -1 : ((i[key] < ii[key]) ? 1 : 0);
}
function ASC(i, ii) { // ASC
return (i[key] > ii[key]) ? 1 : ((i[key] < ii[key]) ? -1 : 0);
}
function StartSort(data, myArray, order) {
// data - row for sorting, array - array fo sorting, order - order of sorting
key = data;
arr = myArray;
if (order.toUpperCase() == "ASC") {
sortedArray = arr.sort(ASC);
} else {
sortedArray = arr.sort(DESC);
}
return sortedArray;
}
//sorting started
const input = [{
id: '1',
name: 'John',
city: 'Denver',
State: 'CO'
},
{
id: '2',
name: 'Smith',
city: 'San Fransisco',
State: 'CA'
},
{
id: '3',
name: 'Adam',
city: 'Concord',
State: 'CA'
},
{
id: '1',
name: 'John',
city: 'Concord',
State: 'CA'
}
]
let output1 = StartSort('state', input, 'ASC');
output1 = StartSort('city', output1, 'ASC');
output1 = StartSort('id', output1, 'DESC');
console.log(output1);

Compare two List of objects and once it matched then return list of objects in React JS

I am very new to ReactJs. I have one requirement to compare both lists and then matched objects should return as output. These comparison should based on unique keys in the object i.e id, endTime.
Please have a look into below code.
From the below two Lists , I need to compare both of them based on unique keys (id, endTime), once it is matched or equal then need to return those objects as list.
Please help me on this.
The selected/ matched objects should return as output.
const apiResponse = [
{
id: 5520437,
startTime: 1498665761714,
endTime: 1498665824487,
},
{
id: 5520436,
startTime: 1498665761714,
endTime: 1498665824488,
},
{
id: 5520435,
startTime: 1498665761714,
endTime: 1498665824489,
},
{
id: 5520434,
start Time: 1498665761714,
endTime: 1498665824490,
}
]
const mySelectedData = [
{
id: 5520437,
start Time: 1498665761714,
endTime: 1498665824487,
},
{
id: 5520436,
start Time: 1498665761714,
endTime: 1498665824488,
},
]
I hope this is what you were looking for. Im comparing both of list and if given element of apiResponse is also an item of selected Data - is added to the result list
const tada = apiResponse.reduce(
(result, item) =>
mySelectedData.some(el => el.endTime === item.endTime)
? [...result, item]
: result,
[]
);
codesandbox -> https://codesandbox.io/s/runtime-smoke-bywti?fontsize=14
You can use filter
function myCompartor(arr){
return function(c){
return arr.filter(function(o){
return o.id == c.id
}).length == 0;
}
}
var common = apiResponse.filter(mySelectedData(mySelectedData));

Mongodb - Create entry to a extisting collection field array

So I have a problem with my var array to add a new chapter, how would I go about this would I have to do this:
array.push({
chapter: [
{
id: 2,
title: 'adsf',
content: '',
authorNotes: 'asdf'
}
]
});
RiTest.ts
import * as mongoose from 'mongoose';
const Scheme = mongoose.Schema;
export const RiTestScheme = new Scheme({
novelName: String,
novelAuthor: String,
novelCoverArt: String,
novelTags: Array,
chapters: [
{
id: Number,
title: String,
content: String,
authorNotes: String
}
]
});
export class RiTestController {
public addChapter(callback: (data) => void) {
var chapterInfoModel = mongoose.model('ChaptersTest', RiTestScheme);
var array = [
{
chapter: [
{
id: 0,
title: 'prolog',
content: 'conetntt is empty',
authorNotes: 'nothing is said by author'
},
{
id: 1,
title: 'making a sword',
content: 'mine craft end chapter',
authorNotes: 'nothing'
}
]
}
];
let newChapterInfo = new chapterInfoModel(array);
newChapterInfo.save((err, book) => {
if (err) {
return callback(err);
} else if (!err) {
return callback(book);
}
});
}
}
This doesn't work, var array doesn't get saved into let newChapterInfo = new chapterInfoModel(array); what I am trying to do add another chapter to array.chapter but the array doesn't get recognized in the chapterInfoModel() how would I fix this array and add an item to the array to create a new entry into this existing collection
thank for taking your time to answer my question.
You are trying to insert array of document to your collections, That the reason its not inserting into your collection.
Document.prototype.save() will insert only one document to your collection depends on your definition. So to insert chapter here is the code below,
//array as Object
var array = {
chapter: [
{
id: 0,
title: 'prolog',
content: 'conetntt is empty',
authorNotes: 'nothing is said by author'
},
{
id: 1,
title: 'making a sword',
content: 'mine craft end chapter',
authorNotes: 'nothing'
}
]
};
//Push to your chapter array
array.chapter.push({
id: 2,
title: 'adsf',
content: '',
authorNotes: 'asdf'
});
let newChapterInfo = new chapterInfoModel(array);

DynamoDb: UpdateExpression for updating arrays

Env: NodeJS service using aws-sdk for interacting with DynamoDb.
Problem: When I set an attribute of an item to an array, it is saved as a string. I expect x: ['1'] but I get x: '1'. I believe this is because I'm incorrectly writing my UpdateExpression/ExpressionAttributeValues.
Situation: I have a table with a field called users. Users is an array of uuids that can be updated. An example of an item in the table:
{ x_name: 'Hello',
owner: '123456',
x_uuid: '1357911',
users: []
}
I want to update the users array with a user uuid. To my update function I pass through:
{ users: ['13245395'] }
The update function (data is { users: ['13245395'] }):
updateX(data, { x_uuid }) {
if (!x_uuid) {
throw new Error('No x_uuid supplied')
}
// new doc client
const docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: this.table,
Key: {
'x_uuid': x_uuid
},
UpdateExpression: "set users = :users",
ExpressionAttributeValues:{
":users": `${data.users}`
},
ReturnValues:"ALL_NEW"
};
return new Promise((resolve, reject) =>
docClient.update(params, (error, x) => {
return error ? reject(error) : resolve(x)
})
)
}
}
The result I get is
{ x_name: 'Hello',
owner: '123456',
x_uuid: '1357911',
users: '13245395'
}
but what I expected was:
{ x_name: 'Hello',
owner: '123456',
x_uuid: '1357911',
users: ['13245395']
}
Previously tried:
wrapping data.users in an array when creating params (works for the first id but the second id added gets appended to the same string as the first so it looks like ['123,456'] instead ['123', '456'].
UpdateExpression: "set users = :users",
ExpressionAttributeValues:{
":users": [${data.users}]
},
Using the "L" and "S" data types to determine that it's an array of strings, i.e.
UpdateExpression: "set users = :users",
ExpressionAttributeValues:{
":users": { "L": { "S":${data.users} } }
},
You are converting your users array to a string
":users": `${data.users}`
Try
":users": data.users
This will set users to the array in data.users

Push to array within subdocument in mongoose

I'd like to push to an array that's within a subdocument in Mongoose/MongoDB.
Here is the schema:
var UsersSchema = new mongoose.Schema({
user: String,
stream: String,
author: String,
tags: Array,
thumb: Number,
added: Number
})
var ContentSchema = new mongoose.Schema( {
title: String,
url: String,
description: String,
text: String,
users: [ UsersSchema ]
})
I'd like to push an array into the UserSchema.tags array for a specific users sub-document.
I have tried this and several variations: https://stackoverflow.com/a/19901207/2183008
By default, my front-end Angular app is sending the tags as an array of objects. So it's
[ { 'text': TAG_STRING_HERE } ]
or
[ { 'text': TAG_STRING_HERE }, { 'text': TAG2_STRING_HERE } ]
But I've also tried just using and array of strings, which I'm fine doing if objects are a problem for some reason.
I have tried this:
var tags = req.body.tags,
contentId = mongoose.Types.ObjectId( req.body.id )
Content.update(
{ 'users._id': contentId },
{ $push: { 'users.tags': { $each: tags } } }
).exec()
.then( function ( result ) {
console.log( result )
resolve( result )
}, function ( error ) {
if ( error ) return reject( error )
})
Which gives me this error:
{ [MongoError: cannot use the part (users of users.tags) to traverse the element ({users: [ { user: "54f6688c55407c0300b883f2", added: 1428080209443.0, stream: "watch", _id: ObjectId('551ec65125927f36cf4c04e9'), tags: [] }, { user: "54f6688c55407c0300b883f2", added: 1428080222696.0, stream: "listen", _id: ObjectId('551ec65e25927f36cf4c04ea'), tags: [] } ]})]
name: 'MongoError',
code: 16837,
err: 'cannot use the part (users of users.tags) to traverse the element ({users: [ { user: "54f6688c55407c0300b883f2", added: 1428080209443.0, stream: "watch", _id: ObjectId(\'551ec65125927f36cf4c04e9\'), tags: [] }, { user: "54f6688c55407c0300b883f2", added: 1428080222696.0, stream: "listen", _id: ObjectId(\'551ec65e25927f36cf4c04ea\'), tags: [] } ]})' }
The solution is below. Note this is using Q and Express. The part of note is the 'users.$.tags. I thought I had tried this but I guess not! I also used $pushAll instead, but $each might also work. My tags is always an array.
var tags = req.body.tags,
contentId = mongoose.Types.ObjectId( req.body.id )
console.log( tags )
Content.update(
{ 'users._id': contentId },
{ $pushAll: { 'users.$.tags': tags } }
).exec()
.then( function ( result ) {
console.log( result )
resolve( result )
}, function ( error ) {
if ( error ) return reject( error )
})

Resources