Cannot fetch array of strings - javascript

Hello i'm trying to fetch some data in my mongoDB collections through graphQL,
Here's my graphQl schema :
type Account {
_id: String
id: String
account_id: Int
limit: Int!
products: [String]
}
type Query {
account(_id: String): [Account]
}
Here's a console log of the resolver return
[ { _id: 5ca4bbc7a2dd94ee58162a49,
account_id: 142442,
limit: 9000,
products:
[ 'Commodity',
'CurrencyService',
'Derivatives',
'InvestmentFund',
'InvestmentStock' ],
id: '5ca4bbc7a2dd94ee58162a49' } ]
but here's the query returns from graphiQL
{
"data": {
"account": [
{
"id": "5ca4bbc7a2dd94ee58162a49",
"_id": "5ca4bbc7a2dd94ee58162a49",
"account_id": null,
"limit": 9000,
"products": null
}
]
}
}
I can't figure out why my products and account_id fields return a null value, they seems to have the correct type, did i miss something ?

Okay nevermind the problem was in the mongoose schema i forgot to add these fields
const accounts = new Schema({
limit: Number,
products: [String],
account_id: Number
})

Related

MongoDB findOne using $and & $elemMatch not working?

I am trying to check if there is an existing conversation between two users before creating another one.
My Conversation object stores the conversation participants in an array, I need to return a conversation object that has BOTH participants (senderId & recId) that exists in my database but I am unable to build to correct MongoDB query to get it.
Please see the queries I have tried below because I have tried all manner of using $and & $elemMatch but can't get it to work.
Thank you
Conversation Model
const conversationSchema = mongoose.Schema(
{
participants: [participantSchema],
},
{timestamps: true}
)
const participantSchema = mongoose.Schema(
{
userId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: `User`,
},
username: {
type: String,
required: true,
}
}
)
Conversation Object
{
_id: 61cb6316asas4b54e09168234,
participants: [
{
userId: 61b777ea6815a69a625b,
username: 'johnsmith'
},
{
userId: 61bc0dcbe7181ccfd806,
username: 'testuser'
}
],
createdAt: 2021-12-28T19:18:46.673Z,
updatedAt: 2021-12-28T23:41:12.364Z
}
Queries I have tried that ARE NOT what I need or don't work
// null - no convo found when the convo definitely exists in db
const existingConvo = await Conversation.findOne({
$and: [{ userId: senderId }, { userId: recId }],
})
// works but only checks for ONE id property
// if I make an array: "Query filter must be an object, got an array"
const existingConvo = await Conversation.findOne({
participants: { $elemMatch: { userId: senderId } },
})
// "Unknown operator $and"
const existingConvo = await Conversation.find({
participants: {
$and: [{ userId: senderId }],
},
})
// returns empty array when it should have the convo object
const existingConvo = await Conversation.find({
participants: { $all: [{ userId: senderId }, { userId: recId }] },
})

GraphQL Cannot Return 'Null' for 'Non-Nullable Field' <Type.Field>

I understand what the error is saying and tried debugging every possible solution.
The exact error in my case is:
{
"errors": [
{
"message": "Cannot return null for non-nullable field Order.total.",
"locations": [
{
"line": 5,
"column": 5
}
],
"path": [
"createOrder",
"total"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"Error: Cannot return null for non-nullable field Order.total.",
In my Type Definitions, I have made the Order.total field nullable and the problem still persists.
Order Type:
# Mutation
createOrder(orderInput: OrderInput!): Order!
# Custom Input
input OrderInput {
username: String!
total: Float
pizzas: [ID]!
collectAt: String!
createdAt: String!
}
The create order resolver has been set up just the same as all other resolvers which work perfectly.
Create Order Resolver:
createOrder: async (
_,
{ orderInput: { username, total, pizzas, collectAt, createdAt } }
) => {
try {
// console.log(pizzas);
const newOrder = Order({
username,
total,
pizzas,
collectAt,
createdAt
});
const res = await newOrder.save();
return res;
} catch (err) {
throw new Error("Caught An Error => " + err);
}
}
I know some people may request what ORM I'm using in this case, I'm using mongoose.
Here is the Order Model:
const orderSchema = new Schema({
username: String,
total: Number,
pizzas: [
{
type: Schema.Types.ObjectId,
ref: "pizzas"
}
],
collectAt: String,
createdAt: String
});
Thanks in advance.

MongoDB: aggregation, array of objects to string value

My question is pretty similar to: MongoDB Aggregation join array of strings to single string, but instead of pure Array, like: ['Batman', 'Robin'] I have Array of objects:
_id: 1,
field_value: [
{
_id: 2,
name: "Batman"
},
{
_id: 3,
name: "Robin"
}
]
I am trying to use $reduce but got error instead.
I want to receive the following result:
_id: 1,
field_value: "Batman, Robin" /** <= String value */
or at least array of property values:
_id: 1,
field_value: ["Batman", "Robin"] /** <= Array of strings (name property) */
My MongoPlayground data example
You need the same approach with $reduce, $$this represents a single field_value entity so you need $$this.name:
db.collection.aggregate([
{
$project: {
field_value: {
$reduce: {
input: "$field_value",
initialValue: "",
in: {
$concat: [
"$$value",
{ $cond: [ { $eq: [ "$$value", "" ] }, "", "," ] },
{ $toString: "$$this.name" }
]
}
}
}
}
}
])
Mongo Playground

How To Push mutiple array elements into mongod by mongoose

There is a mongodb Schema which include these field, its type is array
......
orderlist: [
{
id: String,
price: Number,
photo: String,
name: String,
num: Number
}
]
......
The frontend post me the data such as this,this array has lots of array elements
goodslist:[
{
goodsid: '10001',
goodsprice: 20,
goodsphoto: '/goodsimg/upload_1843.jpg',
goodsname: 'goods1',
goodsnum: 2
},
{
goodsid: '10002',
goodsprice: 30,
goodsphoto: '/goodsimg/upload_1845.jpg',
goodsname: 'goods2',
goodsnum: 4
},
........(etc)
]
what can I do to push this 'goodslist' data into 'orderlist' field by mongoose without changing mongodb field, thanks
You must use mongoose virtuals to achieve this issue.
Your schema must like this:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const collectionName = 'orderlist';
const OrderSchema = new Schema({
id: String,
price: Number,
photo: String,
name: String,
num: Number
}, { minimize: false });
const OrderlistSchema = new Schema({
orderList: [OrderSchema]
}, { minimize: false, toJSON: { virtuals: true } });
OrderlistSchema.virtual('goodslist').
get(function () {
return this.orderList.map(order => ({
goodsid: order.id,
goodsprice: order.price,
goodsphoto: order.photo,
goodsname: order.name,
goodsnum: order.num
}))
}).
set(function (v) {
this.set({
orderList: v.map(good => ({
id: good.goodsid,
price: good.goodsprice,
photo: good.goodsphoto,
name: good.goodsname,
num: good.goodsnum
}))
});
});
module.exports = mongoose.model('Orderlist', OrderlistSchema, collectionName);
goodslist is virtual field here.
With this schema, you can set order field with your format without changing anything in mongodb.
Example posting document:
{
"goodslist": [
{
"goodsid": 2,
"goodsprice": 200,
"goodsphoto": "photo2",
"goodsname": "name2",
"goodsnum": 1234
}
]
}
you can also get order data in goodlist format
{
"_id": "5e9d8c0e27c7a813840c9ff0",
"orderList": [
{
"_id": "5e9d8c0e27c7a813840c9ff1",
"id": "2",
"price": 200,
"photo": "photo2",
"name": "name2",
"num": 1234
}
],
"__v": 0,
"goodslist": [
{
"goodsid": "2",
"goodsprice": 200,
"goodsphoto": "photo2",
"goodsname": "name2",
"goodsnum": 1234
}
],
"id": "5e9d8c0e27c7a813840c9ff0"
}
It going to be something like the following:
//Update order | create if does not exist
orderDB.updateOne({ _id: 'xxxx' }, {
//Push the list into order array
$push: {
orderlist: [{
id: goodlist[0][0],
price: goodlist[0][1],
photo: goodlist[0][2],
name: goodlist[0][3],
num: goodlist[0][4],
}]
}
//Upsert => update / create
}, { upsert: true })
However, you might need to loop through the goodslist.

mongoose mongodb query find

i'm new in mongo and mongoose.
i want to do a simple query in relational database but i have strong problems to do in mongo
here is me schema:
const GroupSchema = new Schema({
name: { type: String , required:true},
image: { type: String , required:true},
location : {type:String , required: true},
locationCode: {type:String , required:true },
created: {type:Date, default:Date.now() , required:true },
createdBy: {type: Schema.Types.ObjectId , ref:'User' , required:true},
//category: [{type:String,enum:[ config.TYPES ]}],
pendingUsers: [
{ text:String,
user:{type: Schema.Types.ObjectId , ref: 'User'}
}
],
rejectedUsers: [ {type: Schema.Types.ObjectId , ref: 'User'} ],
users: [ {type: Schema.Types.ObjectId , ref: 'User' , required:true } ],
adminUsers:[{type:Schema.Types.ObjectId , ref:'User', required:true}],
events :[Event],
activity:[Activity]
})
and i my controller file i want to do the following query:
let groupId = '123123hgvhgj
let userId = 'asdfsadf3434
Group.find()
.where('_id').equals(groupId)
.where('pendingUsers.user')
.in(userId)
.where('users')
.in(userId)
.where('adminUsers')
.in(userId)
.where('rejectedUsers')
.in(userId)
.exec(function (err, records) {
//make magic happen
console.log(records)
});
i have to get the record WHERE _id match with a group id AND (userId exists in pendingUsers OR userid exists in rejectedUsers OR userid exists in users OR userid exists in adminUsers )
i know that seems to be a simple query but returns empty when should be returned the record i have something wrong in the query?
thanks
Even if mongoose seems to support some simple and + or operations (docs)
it seems to me as if you would still need to mix some pure mongodb query into it.
Considering that, i would go with a pure mongodb style query. This one should fit your needs(untested) or will at least point you in the right direction:
Group.find({
$and: [
{_id: groupId},
{
$or: [
{ pendingUsers.user: { $elemMatch: {userId} } },
{ rejectedUsers: { $elemMatch: {userId} } },
{ users: { $elemMatch: {userId} } },
{ adminUsers: { $elemMatch: {userId} } },
]
}
]
});
Group.find({
$and: [
{ _id: groupId},
{
$or: [
{ "pendingUsers": { $elemMatch: { "user": userId } } },
//{ 'pendingUsers.user' : { $elemMatch: {userId} } },
{ rejectedUsers: { $elemMatch: {userId} } },
{ users: { $elemMatch: {userId} } },
{ adminUsers: { $elemMatch: {userId} } },
]
}
]
})

Categories

Resources